# Tag Info

14

Usually, when one defines a function that's not too complex (usually a one-liner) it is customary (here we mean Mathematica custom) to define it directly without any scoping constructs (Module, With or Block). For example: myFunction[x_]:= 2 Sin[x] + Exp[-x^2] But as the function definition gets more complex, instead of polluting the Global context with ...

8

I can't find any confirmations in the documentation, but through numerical and visual checks I think when at least one input to Mod is not real, we have This definition doesn't equal the definition one would think to have over the reals, so my guess is a piecewise definition is used to modify the function over the real line. Testing mod[z_, n_, d_] := z ...

7

Michael E2 is correct in that Max is remarkably capable with functions as well as values. Defining f1[x_] := -2 x + 2 f2[x_] := -x + 1.5 f3[x_] := x - 0.5 f4[x_] := 2 x - 2 funcs = {f1[x], f2[x], f3[x], f4[x]}; We can use Max to get the function of the envelope: m[x_] = PiecewiseExpand@Max[f1[x], f2[x], f3[x], f4[x]] Plot[funcs, {x, 0, 2}, Epilog ...

6

It seems that it will align the column to the first character of the string you put there. So it's an undocumented extension to: "c" - align on the character "c" Column[{ "OffOutputasodas", "OutputOffo", "adssoadasOffOutput" }, "Output" ] Column[{ "OffOutputasodas", "oOutputOffo", "adssoadasOffOutput" }, "output" ]

5

Old versions: Use StringForm[]: example[a_, b_] := Module[{c = a + b}, ToString[StringForm["The solution is ", c]]] example[x, x + 1] "The solution is 1 + 2 x" New versions: Use StringTemplate[]: example[a_, b_] := Module[{c = a + b}, StringTemplate["The solution is sol"] @ <|"sol" -> ToString[c, ...

4

Here is a hint. Consider you want to implement a function $f(n)=2^n$ using NetList. You will do something like this: NestList[#*2&,1,5] So we used the fact that $f(n+1)=2f(n)$. Now we will implement Fibonacci sequence where $g(n)=g(n-1)+g(n-2)$ NestList[{#[[2]],#[[1]]+#[[2]]}&,{0,1},10] ...

3

Apart from StringForm["The solution is ", c], another solution is using StringJoin[](<>) directly. example[a_, b_] := Module[{c}, c = a + b; "The solution is " <> ToString[c] ] Or example[a_, b_] := "The solution is " <> ToString[a + b]

3

All you have to do is give ui the attribute HoldFirst. It also a good idea to make the image an argument of ui. That is, define SetAttributes[ui, HoldFirst]; ui[pts_, img_Image] := LocatorPane[ Dynamic[pts], Dynamic[ Show[ img, Graphics[ {Table[ Arrow[{pts[[i]], pts[[i + 1]]}], {i, 1, ...

3

Since there was no objection to my comment, I'll post it as an answer: By using Infix notation, an operators with arbitrarily many arguments (more than one) can be written as in this example: Range[10]~RandomSample~3 So the operator separates the arguments in the input. If, on the other hand, you want to actually use a re-defined version of the ...

2

Turning my comment into an answer: ClearAll[MapAccumulate]; MapAccumulate[f_, list_List] := f@Take[list, #] & /@ Range@Length@list; Now you can do stuff like this: MapAccumulate[Mean, mylist]

2

Aye carumbah - new versions lis = {x1, x2, x3, x4}; • Sol1: FoldList[Append, {}, lis] // Rest {{x1}, {x1, x2}, {x1, x2, x3}, {x1, x2, x3, x4}} • Sol2: sol = FoldList[Join, lis] /. Join->List {x1, {x1, x2}, {x1, x2, x3}, {x1, x2, x3, x4}} One can then Map any desired function, e.g. Map[Mean, sol] Above work for numerical versions ...

2

Wrap your Monitor call in a Function object endowed with a holding attribute: Table[Pause[n/10], {n, 10}] // Function[{input}, Monitor[input, n], HoldAll] This will work as though you had wrapped Monitor around your Table.

2

You can use Unevaluated Unevaluated@Table[Pause[n/10], {n, 5}] // Monitor[#, n] & The neater way will be to use the infix version as suggested by wxffles in the comments, but your question specifically asked for postfix so... For completeness, here it is Table[Pause[n/10], {n, 5}] ~Monitor~ n

2

Braces [], brackets {}, and parenthesis () all have different meanings in mathematica. Sorry for the post. I will read the documentation and tutorials more completely.

2

I think that value injection using With will work for you. Let's first define the sample g value you want: g = 1 + x^2 Now we define myfunc injecting the current definition of g inside its definition: Clear[myfunc] With[{g = g}, myfunc[f_] := Simplify[f/g]] Now let's change the value of g and check whether the definition of myfunc is affected: g = ...

2

lead[f_Function ] := lead[f@\[FormalX]] lead[f_?PolynomialQ] /; (Length[(v = Variables[f])] == 1) := Last@CoefficientList[f, v[[1]]] lead[2 x^2 - x + 7 ] lead[ 3 #^2 - # + 7 &] 2 3 Note this breaks if you supply a pure function with more than one slot. See here if you need to deal with that: How can I get the number of slots in Function?

1

You can see what you get with the body of your function myInvFun over parameter instances: In[215]:= FindRoot[myfun[x, 2, 5] == 2.0, {x, 0}] Out[215]= {x -> 0.} FindRoot::jsing: Encountered a singular Jacobian at the point {x} = {0.}. Try perturbing the initial point(s). >> So, if you change your definition as advised by the message you'll get ...

1

I think one of the most important core functions is ReplaceAll(/.). I find it useful as some functions output results as rules and then you can use replaceall directly with the results. https://reference.wolfram.com/language/ref/ReplaceAll.html.

1

Maybe something like this? g = 1 + x^2 Unprotect[saveg] saveg = g; myfunc[f_] := Simplify[(f/saveg)] Protect[saveg]

1

If you want to keep the format separate form the function (could be useful in situations where you have many functions like example you want to format like this. Define an UpValue solution that uses TextString solution /: TextString[solution[s_]] := "The solution is " <> ToString[s // TraditionalForm] Now using this definition for example: ...

1

You could also use: cm[f_, s_, t_, n_] := InverseLaplaceTransform[LaplaceTransform[f, s, t]^(n + 1), t, s] (s>0): e.g. TableForm[Table[{j, cm[Exp[-a s] UnitStep[s], s, t, j]}, {j, 1, 10}], TableHeadings -> {None, {"n", "n-fold Convolution"}}]

1

Plot3D[x^2 - y^2 + x*y, {x, -10, 10}, {y, -10, 10}] Which outputs a 3D graph over the specified domains. You have to specify a domain in order to determine minimum or maximum as global minimum/maximum are at positive and negative infinity respectively. It is pretty clear from the equation and the plot that there is a saddle point at (0,0) however.

1

You can use MovingMap. First it is faster to get h with Part rather than with Table. Also it is a bad idea to create variables that begin with a capital letter as you could conflict with built-in symbols. dat = FinancialData["HD", {"Jan. 1, 2015", "Jan. 1, 2016"}]; h = dat[[All, 2]]; You have not provided the definitions for the variables in the ...

1

The trouble with many methods is that they only work on integer inputs. Trying Alexei's answer with approximate numbers {1.0, 1.0 + 2 I, 3.0 - 5 I, 7, 9.0 + I} /. x_ /; Head[x] == Complex -> Sqrt[Re[x]^2 + Im[x]^2]*Exp[I ArcTan[Re[x], Im[x]]] (* {1., 1. + 2. I, 3. - 5. I, 7, 9. + 1. I} *) just spits back out the original answer. Also, a simpler ...

1

Try this: {1, 1 + 2 I, 3 - 5 I, 7, 9 + I} /.x_ /; Head[x] == Complex -> Sqrt[Re[x]^2 + Im[x]^2]*Exp[ArcTan[Re[x], Im[x]]] yielding (* {1, Sqrt[5] E^ArcTan[2], Sqrt[34] E^-ArcTan[5/3], 7, Sqrt[82] E^ArcTan[1/9]} *) Edit: to address your question If you want to have the argument shown as fractions of Pi sa for the angles of 45 grad or 60 ...

Only top voted, non community-wiki answers of a minimum length are eligible