New answers tagged

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

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


0

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: ...


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]


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, ...


0

I would use: Export[ "file.txt", Map[FortranForm, data, {2}], "Table", "FieldSeparators" -> " " ] (* File contents: 0 7.38905609893065 5 0.049787068367863944 10 0.00033546262790251185 15 2.2603294069810542e-6 20 1.522997974471263e-8 25 1.026187963170189e-10 30 6.914400106940203e-13 35 ...


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.


0

Local variables will be put in the first {} in Module[], for example: f[x_] := Module[{}, t = x^2]; f[2] (*--> 4*) t (*--> 4*) You will obtain t=4 after you evaluate the codes above, and if you f[x_] := Module[{t}, t = x^2]; f[2] (*--> 4*) t you will obtain t itself. Namely, t is just a global symbol(don't forget clear value ...


13

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 ...


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"}}]


0

Thanks to all who helped or tried to help me here, finally I found a solution. My special function was the exponential distribution but one can apply it for any arbitrary function. f[x_] = \[Lambda]*Exp[-\[Lambda]*x] UnitStep[x]; g[x_] = \[Lambda]*Exp[-\[Lambda]*x] UnitStep[x]; convi[n_] := {Do[ g[x[i]] = Convolve[f[x[i - 1]], g[x[i - 1]], x[i - 1], ...


2

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


0

I hope someone else explains this better! It seems that expressions don't get automatically evaluated inside the Plot function. Try including an Evaluate function f[x_, N_] := FourierSinSeries[x - 1, x, N]; Plot[{Evaluate@f[x, 1], Evaluate@f[x, 5]}, {x, 0, 3}]


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] ...


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


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 ...


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 ...


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 ...


0

Let f[x_] := {-x+2,-2x+3,1.1x-1,2x-3} A=0; B=3; Plot[Evaluate@f[x],{x,A,B}] With this, Flatten[Ordering[#,-1]&/@f/@ Mean/@Partition[Sort@Flatten[{A,B,x/.Table[NSolve[{f[x][[i]]==f[x][[j]],A<=x<=B},x],{i,1,Length[f[0]]-1},{j,i+1,Length[f[0]]}]}],2,1]]//.{a___,b_,b_,c___}:>{a,b,c} Outputs {2,1,3,4}. This code works also for non-linear ...


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" ]


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 ...


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 ...


0

Since the arguments are rational: v = {1/10, -1 - 2 I, 3 - 5/3 I, 7, 9/10 + I}; Abs[v] Exp[I Arg[v]] {1/10, Sqrt[5] E^(I (-π + ArcTan[2])), 1/3 Sqrt[106] E^(-I ArcTan[5/9]), 7, 1/10 Sqrt[181] E^(I ArcTan[10/9])}


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, ...


2

Besides the plot command (as pointed out by J.M.), you also have a problem with the derivative. Here's one way to fix it: yfunc[M_] := 10^(12 - M); sigmafunc[ M_] := (16.9*(yfunc[M])^0.41)/(1 + 1.102*(yfunc[M])^0.20 + 6.22*(yfunc[M])^0.333); xfunc[M_] := 1.686/sigmafunc[M]; dsigmadM[M_] := (Log[10]*10^M)^(-1)*D[sigmafunc[x], x] //. x -> M; ...


0

The reason for the behavior you were seeing can be seen if you look through the definitions within the package. In particular, the extended elementary functions are defined with a format like func[a:Quaternion[__?ScalarQ]] := (* stuff *) where ScalarQ[] is a private package function that checks if the stuff within the Quaternion[] object are real numbers. ...



Top 50 recent answers are included