# Tag Info

## Hot answers tagged function-construction

5

You could achieve without If, e.g.: f[x_Integer] := StringForm[" is an integer", x]; f[x_] := StringForm[" is not an integer", x]; Test: test = {1, 3, Pi, E, Sqrt[2], Zeta[3], Zeta[-2]} Mapping: Column[f /@ test] yields: Please note IntegerQ[3] is True, however IntegerQ[3.] is False

5

The third parameter allows control of Optional behavior for multiple function definitions. It is not attached to the number of actual arguments passed to the function but rather to the number of arguments that appear in the function definition itself. Consider this example: ClearAll[f]; Default[f, 1, 3] = a1; Default[f, 2, 3] = a2; Default[f, 3, 3] = a3; ...

5

In this instance, I would make your Rounding function work just the way the built in Round function does, where it will thread over lists: Round[{1, 1.3, 0.5, 1.7, 2}] === {1, 1, 0, 2, 2} By adding the Listable attribute to Rounding you can accomplish the same thing: Attributes[Rounding] = {Listable}; Rounding[x_] := If[x - Floor[x] < 0.5, Floor[x], ...

5

You can pass a function (or for that matter, anything!) as an argument to another function and you'll need to modify your code accordingly. See this example: ChebyCoeff[func_, m0_] := Module[{m = m0}, f[t] = func[2 Pi t]; Tn[t] = ChebyshevT[j, 2*t - 1]; wt[t] = 1/Sqrt[t - t^2]; p = Table[Chop[NIntegrate[f[t]*Tn[t]*wt[t], {t, 0, ...

5

I think the first few lines are self explanatory, they just build up a list of rules and wrap them in head dispatch. In this particular example the rules pertain to fitted models and particular test results but the dispatch mechanism is quite general. Basically dispatch here is a way of creating a property extraction mechanism similar in spirit to that of ...

5

suits = {" of Hearts", " of Diamonds", " of Clubs", " of Spades"}; deck = Join @@ Outer[StringJoin, Join[ToString /@ Range[2, 10], {"Jack", "Queen", "King", "Ace"}], suits]; RandomSample[deck, 5] (* {"King of Diamonds", "2 of Spades", "6 of Hearts", "5 of Clubs", "8 of Diamonds"} *)

5

E.g, function squares list elements: l1 = {1, 2, 3, 4, 5} {l1, l2, l3, l4, l5, l6} = NestList[#^2 &, l1, 5] (* {{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}, {1, 16, 81, 256, 625}, {1, 256, 6561, 65536, 390625}, {1, 65536, 43046721, 4294967296, 152587890625}, {1, 4294967296, 1853020188851841, 18446744073709551616, 23283064365386962890625}} *) This ...

4

In addition to Kuba's answer: f4[x_] := Block[{val = (x^3 + 10 x - 5)}, val /; val >= 0] f4 will return unevaluated if the condition is not fulfilled. If it is desirable to get some value returned instead, it is convenient to define it as Indeterminate or Undefined by adding second definition: f4[_] = Undefined Now for f[1] you get 6 and for f[0] ...

3

For this task, you can use the function NestList. It takes a function as its first argument, and a starting list of parameters as the second argument. Here I define the function that corresponds to the iteration in your question: step[{x_, y_, z_, ax_, ay_}] := Module[ {xNew, yNew, zNew, axNew, ayNew}, {xNew, yNew, zNew} = {xNew, yNew, zNew} /. ...

3

For all I know this question might get closed, but I'll attempt an answer anyway, for the cases not covered by Nasser in his comment. The main source of confusion (I'm guessing) for you with the code fragments you posted is that Rule in some places is being used for doing pattern replacement (which is the usual case), and in other places it's the thing ...

3

I think your own method can be refined into something useful: f1[a1_, a2_, a3_] := Module[{test, RM}, test = If[300 < # < 500, #, Return[{}, Module]] &; RM = Table[test[a1 + a2 + a3 + i1], {i1, 1, 10}]; Total @ RM ] Now: f1[100, 101, 102] f1[1, 2, 3] f1[100, 195, 200] 3085 {} {} Note that I used a special syntax of Return to exit ...

3

f[a1_, a2_, a3_] /; (IntervalIntersection[Interval[{300, 500}], Interval[a1 + a2 + a3 + {1, 10}]] === Interval[]) := Module[{RM = Table[a1 + a2 + a3 + i1, {i1, 1, 10}]}, Plus@@ RM] Is probably the most direct way to accomplish this. Called with non-satisfying values, it simply returns unevaluated.

3

There are many ways: f1[x_?NumericQ] := (x^3 + 10 x - 5) /. n_?Negative -> "undefined" or more safe: f2[x_] := With[{val = (x^3 + 10 x - 5)}, If[Negative@val, "undefined", val]] another one for open intervals: f3[x_] := Clip[(x^3 + 10 x - 5), {0, ∞}] /. (0 -> "undefined")

3

Why not just take advantage of built-in, faster means? E.g. to create say five streams of "bits" each with specified transition probabilities: RandomFunction[DiscreteMarkovProcess[1, {{.2, .8}, {.6, .4}}], {0, 20}, 5]["States"] - 1 (* {{0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0}, {0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, ...

3

Is this what you are after? Using WhenEvent to find the extrema.. s = Reap[ NDSolve[{y''[t] + (a + b Cos[t]) y[t] == 0, y[0] == 1, y'[0] == 0, WhenEvent[ y'[t] == 0 && y[t] > 0 , Sow[{ t, y[t]}]]}, y, {t, 0, 50}] ] Show[ Plot[Evaluate[y[t] /. s[[1]]], {t, 0, 50 }, PlotRange -> All], ListPlot[s[[2, 1]], ...

2

I think you want to define f1 as f1 = Function[u, 3 + u] since the form you started with would have created the function and then applied it to x. The easiest way to do what you want is to use Map: Map[f1, list] which has an equivalent syntax f1 /@ list If you really must use a Do loop, you would do it as follows: Module[{result = {}}, ...

2

Immediately after posting I've found the answer to the second question and decided to put it here while the first question still remains unresolved. The thing is that I expected $\it{Mathematica}$ to give -\[Pi] Csc[\[Pi] (1 - z)] when evaluating Simplify[Gamma[z] Gamma[1 - z], TransformationFunctions -> {Automatic, tf2}] but the LeafCount of the ...

1

There seem to be at least two issues here: You are not resetting ExtractionCon = {} inside the outer Do loop, therefore Divided1 grows longer than Partition1 With (1) corrected you will get a different error (repeated): Set::setraw: Cannot assign to raw object 3. >> because the x* Symbols now have values, and they evaluate before the assignment is ...

1

How about something like Clear[fu, fus] With[{uq = Unique["fus"]} , fu[k_] := uq[[k]]; uq = Function[{x}, #] & /@ Table[ Integrate[(x^k Sin[x])/Exp[k x^2], x] , {k, 1, 5} ] ] I picked this example because this integral does not produce a nice result for a fixed k. It can be reasonable to want to make a few functions here, as ...

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