Tag Info

New answers tagged

0

f[n_] := Module[{i = 1}, Nest[(n - i++)/(a + #) &, n, n - 1]]


5

You can use Fold instead: f[n_Integer] := Fold[#2/(1 + #) &, n, Reverse@Range[n - 1]] f[3] $\frac{1}{1+\frac{2}{1+3}}$ It not very useful analytically, but it allows you to invoke the CPU gods: f /@ Range[50] // ListLinePlot[#, PlotRange -> All] &


3

Could it be that you're looking for the function Fold rather than Nest? Fold[f[n - #2, #1] &, a, Range[5]] f[-5 + n, f[-4 + n, f[-3 + n, f[-2 + n, f[-1 + n, a]]]]]


1

I'm afraid I don't know what realized covariance means. Perhaps the easiest solution is to use RLink and directly use the R implementation. Here are some links to the documentation to get you started. http://reference.wolfram.com/language/RLink/guide/RLink.html http://reference.wolfram.com/language/RLink/tutorial/UsingRLink.html


1

Last @ Nest[{ #[[1]] - 1, f1[ ff[ #[[1]], #[[2]] ] ] } &, {n, list}, 3] f1[ff[-2 + n, f1[ff[-1 + n, f1[ff[n, list]]]]]]


4

NDSolve and now, in V10, DSolve, too, can handle differential equations with discontinuous coefficients. The coefficients have to be in terms of functions that will be recognized as discontinuous. Procedural programs such as in the definition of eqilValue above will not be recongnized and will be treated as numerical black boxes. Piecewise will be ...


3

I believe it's better to cope with discontinuous functions in NDSolse[] by using WhenEvent[] and DiscreteVariables ->. In your case: tauArr = {50, 100, 150, 200, 250}; whens = WhenEvent @@@ MapIndexed[{t == #1, ep[t] -> If[#2 == {5}, -5, #2[[1]]]} &, tauArr] sol = NDSolve[{f''[t] + gamma*f'[t] + kappa*(f[t] - ep[t]) == 0, whens, ...


2

As an improvement to Leonid's answer I've added a helper function, to avoid the Function syntax everytime. l={}; ClearAll@ffff; Options[ffff]={"a":>l}; heldOptionAppendTo[x_]:=Function[{v},AppendTo[v,x],HoldFirst]; ffff[x_,OptionsPattern[]]:=OptionValue[Automatic,Automatic,"a",heldOptionAppendTo[x]] Some other similar useful functions. The great ...


9

I'd do something like this: l = {}; ClearAll@ffff; Options[ffff] = {"a" :> l}; ffff[x_, OptionsPattern[]] := OptionValue[Automatic, Automatic, "a", Function[v, AppendTo[v, x], HoldFirst]] That is, use RuleDelayed instead of Rule to define your held option, and then use extended syntax of OptionValue.


1

One thing to look into to better understand MMA programming is how to define functions with variable arity (number of arguments). The normal way to define a function of x is f[x_]:=.... Functions of two variables are f[x_,y_]:=... etc. However when you don't know the number of arguments beforehand you can use __ (two underscores) and ___ (three underscores) ...


0

Perhaps something like colors = {Red, Blue, Green, Black, Orange, Blue, Pink}; aA = Sin[t] RandomInteger[{1, 7}, {7}]; table = Table[{{aA[[k]] + 1, k}, {aA[[k]], k}, {aA[[k + 1]], k + 1}, {aA[[k + 1]] + 1, k + 1}}, {k, 1, 6}]; polygons = Polygon /@ Prepend[table, Join[{{0, 0}, {1, 0}}, table[[1, ;; 2]]]]; Animate[Evaluate@ ...


1

Thank you for a well-written question with complete code that made this reasonable to answer. Welcome to Mathematica Stack Exchange. :-) You do not need UpValues definitions here. That would only apply if you were attempting to add a rule to e.g. Plus rather than Sup, yet your use of TagSetDelayed makes it clear that you are attaching the rule to Sup. ...


4

I am not sure whether the intention is vectors with all positive entries. If not then potential pairs (a,-a) will also be division by zero. Here is another implementation of formula, removing zero denominators: cd[u_, v_] := Module[{pos, us, vs}, pos = Position[u + v, _?(# != 0 &)]; us = Extract[u, pos]; vs = Extract[v, pos]; Total[(us - ...


3

Just use Evaluate. Let's define F[x_] := (x D[EllipticTheta[3, 0, E^(-Pi x)], {x, 2}] + (3/2) D[ EllipticTheta[3, 0, E^(-Pi x)], {x, 1}]) x^(5/4) First we plot the function on the real line Plot[Evaluate[F[x]], {x, -2, 4}] (* 141107_Plot _F (x).jpg *) Now the 3D-plot for Re, Im, and Abs, respectively Plot3D[Evaluate[Re[F[z] /. z -> x + I ...


3

Use Set ( = ) rather than SetDelayed ( := ) so that the derivatives are carried out before x has a value. F[x_] = (x D[EllipticTheta[3, 0, E^(-Pi x)], {x, 2}] + (3/2) D[ EllipticTheta[3, 0, E^(-Pi x)], {x, 1}]) x^(5/4); F[1.] 0.446697


3

Here is a method that is not exactly to your specification but which may serve you anyway. It uses a Symbol argument. Because a Symbol is passed it is automatically modified by Module and does not need the guesswork of $ModuleNumber etc. but it does need string conversion and cleanup. SetAttributes[printVal, HoldFirst]; printVal[s_Symbol] := Print @ ...


1

I have found the answer to my question. It is because I was trying to modify the value of x, rather than the name itself. A way round this is to make a copy of the variable, and operate on this. This below works: addHeg[aPopulationImmutable_, aNode_, aNumHeg_] := Module[{aPopulation = aPopulationImmutable}, aPopulation[[aNode]][[3]] += aNumHeg; ...


1

One way to think about this syntax is in terms of "functions with parameters that accept inputs". The normal way this is written is function[inputs..., parameters...] this structure leads to a somewhat awkward syntax when mapping over a list of inputs: list = {1.234, 5.678}; (Round[#1, 0.1] & ) /@ list (* {1.2, 5.7} *) The two-argument-list ...


3

Here is a way using delayed evaluation and recursive pure function. It allows to only use one variable: ClearAll[var]; var := Function[Null, With[{res = ! #2}, # := #0[#, res]; res], HoldFirst][var, False] So that: var (* True *) var (* False *) var (* True *)


12

flip = True; var := flip = Not[flip] var (*False*) var (*True*) var (*False*)


1

Let's convert dates: data ={ {{2007, 1, 3}, 0.2}, {{2007, 1, 4}, 0.1}, {{2007, 1, 5}, 0.14}, {{2007, 1, 8}, 0.}, {{2014, 10, 17}, -0.2}, {{2014, 10, 18}, 0.2}, {{2014, 10, 19}, 0.2}}; data = MapAt[DateList, data, {;; , 1}]; I don't know if this can be done automatically but unless I put there the very first day of the week with "neutral" value then ...


4

Using the function by Heike from Determining the week of a year from a given date which gives the week number from a date: data = {{{2007, 1, 3}, 0.2}, {{2007, 1, 4}, 0.1}, {{2007, 1, 5}, 0.14}, {{2007, 1, 8}, 0.}, {{2014, 10, 17}, -0.2}, {{2014, 10, 18},0.2}, {{2014, 10, 19}, 0.2}}; (*replace date with week number for each entry*) data2 = ...


2

If you need to preserve the definitions of a Symbol with exception(s) to some particular case(s) you should use Internal`InheritedBlock: f[a_, b_] := a + b; Internal`InheritedBlock[{f}, f[1, 2] := ab; Table[f[i, j], {i, 1, 2}, {j, 1, 2}]] f[1, 2] {{2, ab}, {3, 4}} 3


1

You can localize the symbol f, and then set f[1,2]=ab as part of the main body of the Block. Block[{f}, f[1, 2] = ab; Table[f[i, j], {i, 1, 2}, {j, 1, 2}] ]


4

Just 'hiding' heads: fq[ex_, a_, h_] := FreeQ[ex /. h[x__] :> Unique["x"], a], e.g. fq[b^2 + f[a] + g[a, x] + h[b, x, a], , (f | g | h)] yields True


8

pf[n_] := PrecedenceForm[CenterDot @@ Table[If[FactorInteger[Abs[n]][[m, 2]] < 2, FactorInteger[Abs[n]][[m, 1]], Superscript @@ (FactorInteger[Abs[n]][[m]])], {m, 1, Length@FactorInteger[Abs[n]]}], 10000]; f2[n_] := If[PrimeQ[Abs[n]] == True, n, Sign[n] pf[n]] f2[-96] See also: PrecedenceForm tutorial / Operator Input Forms >> ...


2

The point is that List has the special property that the head doesn't disappear even with one element. So the solution (as belisarius noted in the comment) is to make the code generate expr with Head=List instead of Plus i.e, expr = List[a[i],b[i],c[i],d[i]] Then I can safely pass this to Map as in Map[f, expr], and it doesn't matter how many elements f ...


1

The following gets the first two steps: m = {{1, 2}, {0, 2}, {3, 2}, {0, 2}, {0, 2}, {0, 2}, {4, 2}}; v = 1 - Unitize[m[[All, 1]]] (*{0,1,0,1,1,1,0} *) vv = Flatten@(Accumulate /@ Split@v) (* {0,1,0,1,2,3,0} *) Update: ... the last step ClearAll[ff]; ff[0] = 1; ff[n_] := Piecewise[{{ff[n-1] m[[n, 2]], vv[[n]] > 1}, {m[[n, 2]], vv[[n]] == 1}}, m[[n, ...


2

Cause As Junho Lee explains this is due to the Listable attribute of Plus which takes effect before other rules. The Standard Evaluation Procedure tutorial says: As soon as the Wolfram System has evaluated the head of an expression, it sees whether the head is a symbol that has attributes. If the symbol has the attributes Orderless, Flat, or ...


1

The message is due to Listable attribute. I guess that the Listable process call Thread. If you get rid of this attribute, do like this. Unprotect[Plus]; Plus[x_List, y_List] := Join[x, y] // Merge[Total] Protect[Plus]; ClearAttributes[Plus, Listable] Have try this. {<|"a" -> 1|>, <|"b" -> 2|>} + {<|"a" -> 2|>} <|"a" ...


3

I'm not sure I understand what you are doing but the plots plat should be simple. We put all your results in a Table using ParallelTable to use all available cores in your CPU. data = ParallelTable[ Block[ {r = f[x, cb], fmax, xmax}, fmax = First[r]; xmax = (x /. Last[r]); {cb, xmax, fmax} ], {cb, 0, 0.3, 0.3/20}]; TableForm[N@data, ...


0

I understand that you are looking for sort of a conditional updating: only when the displayed portion of the variable test changes. My feeling is that it is unavoidable that the displayed portion of test has to be recomputed at each change of test. But that can be done outside Dynamic. Here is an smaller example, showing the sort of solution I am thinking ...


2

Applying the method I described in: How to define even permutations correctly?: fW[a_List] := With[{p = Permutations @ Range @ Length @ a}, Dot[Signature /@ p, func /@ Extract[a, p ~Partition~ 1]] ] This is faster than both of kguler's functions (which are in turn faster than belisarius's code): f1 = Function[{k}, Total[Map[Signature[ #[[Ordering ...


3

mapping[set_] := Dispatch@Thread[set -> Range@Length@set] input = {a, b, c}; Total[Map[Signature[# /. mapping[input]] function[#] &, Permutations[input]]] (* function[{a, b, c}] - function[{a, c, b}] - function[{b, a, c}] + function[{b, c, a}] + function[{c, a, b}] - function[{c, b, a}]*)


6

input = {b, a, c}; Perhaps Total[Map[Signature[ #[[Ordering @ input]] ] func[#] &, Permutations[input]]] (* -func[{a, b, c}] + func[{a, c, b}] + func[{b, a, c}] - func[{b, c, a}] - func[{c, a, b}] + func[{c, b, a}] *) or Total[Map[Signature[#]Signature[input] func[#] &, Permutations[input]]] (* same result *) or Signature[input] ...


3

The problem is, that the Head of series is now Dynamic. With a simple Part you get rid of this: ListPlot[series[[1]], PlotLabel -> title] Besides, why not using Manipulate? Your problem seem perfectly suited for this: Manipulate[ forest = Range[30]; energy = Reverse[Range[30]]; Switch[choice , 1, series = forest; title = "Tree Growth" , 2, ...


1

Generally, Dynamic is used in two ways: Re-evaluating expressions when they would change, and displaying the result. The syntax is Dynamic[expr]. Dynamic[expr] cannot be used to represent the value of expr in different context and calculate with it. It is only used to display it. Allowing controls to change values of variables. Typical syntax: ...



Top 50 recent answers are included