Tag Info

New answers tagged


Here are three ways to get it working using If in the definition of the function. 1) Using Defer myFunc[e_Integer] := If[e > 0, e*e, Defer @ myFunc[0]] 2) Using HoldForm myFunc[e_Integer] := If[e > 0, e*e, HoldForm @ myFunc[0]] 3) Using Inactivate myFunc[e_Integer] := If[e > 0, e*e, Inactivate @ myFunc[0]]


When you type e_ or e_Integer you are using a pattern. You can put constraints on the pattern as well using the ;/ operator myFunc[e_Integer /; e > 0] := e*e myFunc[0] myFunc[17] (* myFunc[0] *) (* 289 *)


Have a look at Table and try; f[x_] := (x + 1)/Sin[x] myTable = Table[f[x], {x, 1000}]; ListPlot[myTable]


More recent (10.1+) versions of Mathematica feature the SequencePosition function, which can be told to stop after the first match, like so: SeedRandom[1337]; a = RandomInteger[{1, 10}, 10000]; b = {1, 7, 1}; SequencePosition[a, b, 1] // AbsoluteTiming (* {0.000175, {{88, 90}}} *) This is quite a bit faster than the MemberQ/Partition-based approach: ...


This is one area where Mathematica's support for functional programming idioms is helpful. Using higher-order functions (in this case FixedPoint, which always evaluates its function argument at least once) we can replace the While loop with a single line, and then use the Listable attribute to thread over lists passed in as inputs, which nicely separates ...


Jack,I want thank you for your answer, it didn't give what I wanted but it was in the right direction. Here is the solution I have (one Do loop and a recursive function call) I could have done it with just one function but this was easier to debug, here are the functions: Do[loop++; map[[loop]] = tablefunction[r, 2, inc, dim, range], {r, range[[1, 1]], ...


Table should do the trick. Table[myfunction[{r, theta, phi}], {r, minr, maxr, dr}, {theta, 0, π, dthe}, {phi, -π, π, dphi} ] This will produce a nested list of myfunction values. You may need to use Flatten on the result depending upon the shape that you want for the output.


I needed to do this well but needed functions for reuse. These functions only need the list of rules that define the new columns. They perform the rule substitution calculate the column values and build the new column rules. Function to calculate named column from rule definition and named column dataset row (Association) ClearAll[calcColumn] ...


KeyValueMap: ds = <|key1 -> list1, key2 -> list2, keyn -> listn|>; KeyValueMap[f] @ ds $\ ${f[key1, list1], f[key2, list2], f[keyn, listn]} ds = <|key1 -> {x1, y1}, key2 -> {x2, y2}, keyn -> {x3, y3}|>; KeyValueMap[f[#1, #2[[1]]] &, ds] $\ ${f[key1, x1], f[key2, x2], f[keyn, x3]}


f = {f1, f2, f3}; x = {x1, x2, x3}; Block[{i = 0}, List @@ (f /. s_Symbol :> s[x[[i++]]])] {f1[x1], f2[x2], f3[x3]}


Needs["GeneralUtilities`"] MultiMapAt[Range[3], {f1, f2, f3}][{x1, x2, x3}] $\ ${f1[x1], f2[x2], f3[x3]} Which is equivalent to using (Composition @@ MapThread[MapAt, {{f1, f2, f3}, Range[3]}])[{x1, x2, x3}] Or MapIndexed[{f1, f2, f3}[[First@#2]]@#1 &, {x1, x2, x3}] $\ ${f1[x1], f2[x2], f3[x3]} Or #1[#2] & @@@ Thread[{{f1, f2, f3}, ...


Inner[#1[#2] &, {f1, f2, f3}, {x1, x2, x3}, List] (* {f1[x1], f2[x2], f3[x3]} *) #[[1]][#[[2]]] & /@ Transpose[{{f1, f2, f3}, {x1, x2, x3}}] (* {f1[x1], f2[x2], f3[x3]} *)

Top 50 recent answers are included