wolfram-mathematica

Optional named arguments in Mathematica

匆匆过客 提交于 2020-01-12 01:14:34
问题 What's the best/canonical way to define a function with optional named arguments? To make it concrete, let's create a function foo with named arguments a , b , and c , which default to 1, 2, and 3, respectively. For comparison, here's a version of foo with positional arguments: foo[a_:1, b_:2, c_:3] := bar[a,b,c] Here is sample input and output for the named-arguments version of foo : foo[] --> bar[1,2,3] foo[b->7] --> bar[1,7,3] foo[a->6, b->7, c->8] --> bar[6,7,8] It should of course also

Force function evaluation on declaration

扶醉桌前 提交于 2020-01-11 11:17:09
问题 I have a function f[x_,y_,z_]:=Limit[g[x+eps,y,z],eps->0]; and I plot f[x,y,z] in the next step. Earlier, I used to evaluate the limit and copy the expression in the definition of f . I tried to make it all in one step. However, the evaluation of the Limit is done only when I try to plot f . As a result, every time I change around the variables and replot, the limit is evaluated all over again (it takes about a min to evaluate, so it becomes annoying). I tried evaluating the limit first, and

Force function evaluation on declaration

柔情痞子 提交于 2020-01-11 11:17:07
问题 I have a function f[x_,y_,z_]:=Limit[g[x+eps,y,z],eps->0]; and I plot f[x,y,z] in the next step. Earlier, I used to evaluate the limit and copy the expression in the definition of f . I tried to make it all in one step. However, the evaluation of the Limit is done only when I try to plot f . As a result, every time I change around the variables and replot, the limit is evaluated all over again (it takes about a min to evaluate, so it becomes annoying). I tried evaluating the limit first, and

Difference between == and === in Mathematica

瘦欲@ 提交于 2020-01-10 18:24:07
问题 I was under the impression that = is an assignment, == is a numeric comparison, and === is a symbolic comparison (as well as in some other languages == being equal to and === being identical to . However, looking at the following it would appear that this is not necessarily the case... In: x == x Out: True In: x === x Out: True In: 5 == 5 Out: True In: 5 === 5 Out: True In: x = 5 Out: 5 In: 5 == x Out: True In: 5 === x Out: True In: 5 5 == 5x Out: True In: 5 5 === 5x Out: True In: x == y Out:

Equation of a helix parametrized by arc length between two points in space

左心房为你撑大大i 提交于 2020-01-10 05:28:25
问题 What is the equation of a helix parametrized by arc length (i.e. a function of arc length) between any two points in space? Is there any function for this ? How do i implement the same using matlab or mathematica ? 回答1: just to add to Mitch Wheat's answer, helices are not unique; for a given axis, the degrees of freedom are distance between turns, radius, and phase ( P , A , and phi below) if you generalize to w = 2*pi/P r(t) = (A cos (wt-phi)) i + (A sin (wt-phi)) j + (t) k then one way to

How can I make a “working” repeating decimal representation of a rational number?

╄→尐↘猪︶ㄣ 提交于 2020-01-09 19:27:28
问题 I've figured out how to display the repeating part of a repeating decimal using OverBar. repeatingDecimal doesn't actually work as a repeating decimal. I'd like to make a variation of it that looks and behaves like a repeating decimal. Question How could I make a working repeating decimal representation (possibly using Interpretation[] )? Background Please excuse me if I ramble. This is my first question and I wanted to be clear about what I have in mind. The following will "draw" a repeating

Consistent size for GraphPlots

瘦欲@ 提交于 2020-01-09 13:04:40
问题 Update 10/27 : I've put detailed steps for achieving consistent scale in an answer. Basically for each Graphics object you need to fix all padding/margins to 0 and manually specify plotRange and imageSize that such that 1) plotRange includes all graphics 2) imageSize=scale*plotRange Still now sure how to do 1) in full generality, a solution that works for Graphics consisting of points and thick lines (AbsoluteThickness) is given I'm using "Inset" in VertexRenderingFunction and

How to abort evaluation of a sequence of inputs?

自闭症网瘾萝莉.ら 提交于 2020-01-09 10:55:48
问题 By default pressing Alt + . or calling Abort[] within the evaluation causes abort of the currently evaluating input. But when working in the FrontEnd we usually send to the kernel a sequence of inputs. For example, if we type the following three expressions on separate lines in one Cell and then press Shift + Enter we get infinite evaluation: f := CheckAbort[Pause[.1], Abort[]] While[True, f] While[True, f] While[True, f] To stop this infinite evaluation we must to press Alt + . three times.

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

雨燕双飞 提交于 2020-01-09 04:38:10
问题 I'm bewildered by all the built-in Mathematica functions that purport to prevent evaluation in some way: Unevaluated , Defer , Hold , and over half a dozen of the form Hold* . The Mathematica documentation just explains each function in isolation without explaining why you would choose one or the other. Can anyone offer a coherent explanation of all these functions? The whole thing seems like a convoluted mess to me. Relating it all to Lisp macros might be a good place to start. Most of the

Algorithm to find the determinant of a matrix

可紊 提交于 2020-01-07 02:17:10
问题 I have to write an algorithm to find the determinant of a matrix, which is done with the recursive function: where A_ij is the matrix, which appears when you remove the i th row and the j th column for A . When A has dimension n x n , then the dimension for A_ij is (n-1) x (n-1) . I'm not allowed to use Minor[] or Det[] . How do I write this algorithm? This is the code I have so far: det1[Mi_ /; Dimensions[Mi][[1]] == Dimensions[Mi][[2]]] := Module[{det1}, det1 = Sum[ If[det1 == 1, Break[], (