The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Tag Info

New answers tagged function-construction

3

Clear["Global*"]; f[x_] = x E^-x - 1; (* Note E vice e *) XY = {#, f[#]} & /@ Range (* {{1, -1 + 1/E}, {2, -1 + 2/E^2}, {3, -1 + 3/E^3}, {4, -1 + 4/E^4}} *) p4[x_] = Fit[XY, {1, x, x^2, x^3, x^4}, x] (* -0.589552 - 0.0147616 x - 0.0248196 x^2 - 0.00460293 x^3 + 0.00161548 x^4 *) Plot[{p4[x], f[x]}, {x, 0, 4}, PlotStyle -> {Green, Red}, ...

4

First, a basic mistake: in Mathematica, the natural log base is entered as E (or as Esc e Esc), and not plain e. And many folks prefer Exp[x] to E^x. Also, your lines f;, f;, etc., since they end with semicolons, just suppress the output from them. Second, even with that corrected, everything is fine until your final Show, where you have so ...

2

Mathematica is written in C/C++ itself. That means it cannot run faster than C/C++. (Assuming of course the faster implementations are used for each algorithm).

0

I have already answered on the main issue for your follow-up question. Here I wish to note that your data have non-positive values for the ordinate, while the function is positive in the given data range 0 < x < 1: fun[r_Real, l_Real] := Sum[a[1/r, l]^n, {n, 1, NNfun[r, l] + 1}] Show[Plot[fun[x, .3], {x, 0, 1}], ListPlot[data], PlotRange -> All] ...

2

You should define your objective function using the "black box" pattern in order to prevent symbolic processing by NonlinearModelFit: fun[r_?NumericQ, l_?NumericQ] :=Sum[a[1/r, l]^n, {n, 1, NNfun[r,l]+1}] Actually such questions were asked already many times on this site, see this FAQ answer: What are the most common pitfalls awaiting new users? This ...

66

High-level languages, like Mathematica, have a high overhead for executing each command/instruction. However, they also typically include commands/instructions that solve a larger and more complex task than those in low-level languages. To take a concrete example, in C, we can add two numbers. In Mathematica, we can add two arrays directly. If we want to do ...

6

Yes, putting the function call in twice will cause it to be evaluated twice. You can visualize this by putting a Print inside the function, for example: f[x_] := (Print["In f"]; x) g[x_] := f[x]^2 + 2*f[x] + 1 g (* In f *) (* In f *) (* 2 *) You can see that it went to f twice. To avoid that, you can assign a temp variable: g2[x_] := Module[{t =...

4

You could do something like G[x_] := a^2 + 2 a + 1 /. a -> F[x] or G[x_] := #^2 + 2 # + 1 &@F[x]

4

SetAttributes[compile, HoldFirst]; compile[code_] := With[{cc = code}, Compile[{{a, _Real}, {b, _Real}, {x, _Real, 1}}, cc] ]; cf1 = compile[N[a*x + b]]; cf1[2.3, 4.7, {1, 2, 3, 4}] cf2 = compile[N[a*x^2 - b]]; cf2[2.3, 4.7, {1, 2, 3, 4}] {7., 9.3, 11.6, 13.9} {-2.4, 4.5, 16., 32.1} Edit Maybe this is rather more what you are looking for? ...

1

Try the following code: xx = {x, x}; hex = x + x; Module[{tmp1}, tmp1 = Replace[xx, a1_ :> Module[{xx}, xx], {1}]; SetDelayed @@ ({h[Pattern[#, _] & /@ xx], hex} /.MapThread[Rule, {xx, tmp1}]) ]; h[{1, 2}]

1

Use a Condition: g[x_] := Module[{res = f[x]}, res /; res < 10] g[x_] := 0

2

You can use Piecewise: g1[x_] := Piecewise[{{x^2, x^2 <= 10}}] or g2[x_] := If[x^2 <= 10, x^2, 0]

6

I do not know a way to make ExperimentalNumericalFunction listable itself, but depending of your goals you can achieve the same behavior in several ways. The simplest way is to create a proxy Symbol with Listable attribute: ClearAll[f, fL] SetAttributes[fL, Listable] fL[x_] := f[x] f = ExperimentalCreateNumericalFunction[{x}, x^2, {}, {_}, ...

1

Here's an approach to match only the stuff inside the brackets (otherwise, it is equivalent to the approach in the question): Attributes[BalancedBracketPattern] = {HoldFirst}; Module[ {defName}, Quiet[ BalancedBracketPattern[name_Symbol: defName, left_: "{", right_: "}"] := Shortest[ left ~~ (name___) ~~ right /; StringCount[name, left] ==...

0

I think you want something like this: f[q_]:= Module[{g}, g[x_]:= Evaluate[q]; g[q]] Your original code does not evaluate your function g[].

1

In the definition of modes, you set a new value for U here: U = (U /. Table[var2[[i]] -> nn[[i]], {i, 1, Length[var2]}]); Any subsequent calls to mode will simply return this value. Try instead modes[j_] := Module[{nn}, nn = Last[ Transpose[ Last[SingularValueDecomposition[Rarz /. \[Omega] -> s2[[j]]]]]]; U /. Thread[var2 -> nn] ] ...

1

GPrPo[x_] := 354 x + 1143 x^2 + 2320 x^3 + 3811 x^4 + 5441 x^5 + 6403 x^6 + 6829 x^7 + 6658 x^8 + 5571 x^9 + 4737 x^10 + 3560 x^11 + 2741 x^12 + 2174 x^13 + 1579 x^14 + 1120 x^15 + 789 x^16 + 502 x^17 + 275 x^18 + 215 x^19 + 117 x^20 + 59 x^21 + 30 x^22 + 30 x^23 + 21 x^24 + 6 x^25 + 4 x^26 + x^27 + 2 x^28 + 3 x^29 + x^30 + 2 x^31 GDPrPo[x_] := D[...

0

Your code works fine for me, but you can get a more concise formulation by writing it like so: Clear[PlaneTrussElement] PlaneTrussElement[e_, A_, {p1 : {_, _}, p2 : {_, _}}] := Module[{ls, ms, L}, L = Norm[p2 - p1]; {ls, ms} = Normalize[p2 - p1]; e A/L {{ls^2, ls ms, -ls^2, -ls ms}, {ls ms, ms^2, -ls ms, -ms^2}, {-ls^2, ...

3

l1 = {2, 2, 3, 3, 7}; M = 4; l2 = Table[Length[Select[l1, # < n &]], {n, 1, M}] {0, 0, 2, 4} UnitStep + Total Total[1 - UnitStep[l1 - #] & /@ Range[M], {2}] (* or Total[1 - UnitStep[l1 - #]] & /@ Range[M] *) {0, 0, 2, 4} Clip + Unitize + Total Total[1 - Unitize@Clip[l1, {#, ∞}, {0, 1}] & /@ Range[ M], {2}] (* or Total[1 - ...

6

list = RandomReal[{0, 100}, 100000]; Accumulate[BinCounts[list, {0, Ceiling[Max[list]], 1}]]

6

The BoolEval package can be used here to get a good combination of performance and clean syntax: << BoolEval Table[BoolCount[list < n], {n, 5}] {0, 0, 2, 4, 4}

3

Noticing that going row-by-row, we're essentially counting in binary (with "min" as 0 and "MAX" as 1), we see that we can use BitAnd[i - 1, 2^(n - j)] == 0 as condition: n = 3; tuples = ConstantArray[0, {2^n, n}]; For[i = 1, i <= 2^n, i++, For[j = 1, j <= n, j++, If[BitAnd[i - 1, 2^(n - j)] == 0, tuples = ReplacePart[tuples, {i, j} -> "...

3

Lukes answer is excellent but it can be improved. This version is faster as well as more concise. n = 3; tuples = ConstantArray[0, {2^n, n}]; Do[ tuples[[i, j]] = If[BitAnd[i - 1, 2^(n - j)] == 0, "min", "MAX"], {j, n}, {i, 2^n}]; tuples // MatrixForm Your For-loop solution can also be improved by applying the same ideas. flag = 0; Do[ If[flag == 0,...

Top 50 recent answers are included