# Tag Info

2

I though it is nice untill I had to add reparse function :) SetAttributes[mySequence, HoldAllComplete]; mySequence[args__] := RawBoxes[ MakeBoxes[{args}][[1, 2]] ]; reparse = ToExpression @* FrontEndExecute @* FrontEndReparseBoxStructurePacket @* ToBoxes y = 7; Hold[{1, x, 2, x, 3}] /. x :> RuleCondition @ mySequence[y, 1+2] //reparse Hold[{1, ...

3

I closed this as a duplicate of Why does this pattern with Plus not work for numbers? but I think I have something that is general enough to be useful, and it's not applicable to that question. We may observe that although a plain use of Block[{Plus}, . . .] does not prevent numeric evaluation of Plus we can still make a substitution that does: Block[{Plus ...

7

I think my answer to Why does this pattern with Plus not work for numbers? is also the answer here. See Plus in the reference manual: Unlike other functions, Plus applies built-in rules before user-defined ones. As a result, it is not possible to make definitions such as 2+2=5. The ability for user-defined rules to supersede built-in ones was ...

5

Let's assume you really can only evaluate your function evals for numeric values. Then the approach of using Evaluate or removing ?NumericQ is not helping. For this situation, I see two possible solutions from the top of my head. Using memoization suppress recalculation evals[x_?NumericQ] := evals[x] = Eigenvalues[{{x^2, 2 x}, {x, 3 x^2}}]; ...

1

To make Plot aware that it's a list: evals[x_] = Eigenvalues[{{x^2, 2 x}, {x, 3 x^2}}] Plot[Evaluate@evals@x, {x, -1, 1}, PlotStyle -> {Red, Green}]

6

Body of Manipulate is wrapped by Dynamic and Dynamic doesn't know what's inside inner Dynamics, that's how we can screen a variable to not prompt the very outer Dynamic to evaluate: Manipulate[ e = RandomVariate[NormalDistribution[0, sigma], n]; {Dynamic@a, e}, {{n, 3}, 1, 5, 1, Appearance -> "Labeled"}, {{sigma, 1}, 1, 2, Appearance -> ...

1

Here's a possible solution. It's working (really well) for 1 particle, but needs to be checked for two particles, I guess due to minor mistakes. Basically, I changed approach and turned to a matrix notation. Let's start from the one-particle system. We simply write the state as a vector, and the operators as matrices, being very careful about the indexing. ...

3

This is messy and imperfect, but it will work in simple cases: Import the package contents: pack = ImportString[data, {"Package", "HeldExpressions"}] (* {HoldComplete[BeginPackage["Test"]], HoldComplete[testFunction::usage = "-";], HoldComplete[Begin["Private"]], HoldComplete[testFunction[] := {123, explicitvalue, Hold[explicitvalue]};], ...

5

We can replace something, in all definitions associated with a symbol, using function like this: ClearAll[replaceExtendedDefinition] SetAttributes[replaceExtendedDefinition, HoldFirst] replaceExtendedDefinition[sym_, rules_] := Replace[ Language`ExtendedDefinition[sym] , (rule:Rule | RuleDelayed)[lhs_, rhs_] :> ...

2

You can use Inactivate with TraditionalForm. Inactivate[h = (R.omega + x).x] // TraditionalForm Inactivate prevents the operations from executing and TraditionalForm gives the formatted output. Hope this helps.

0

Thank you for your replies, they will be useful in the future. I found the solution to my specific problem however. One can use: Distribute[(HoldForm[R].HoldForm[omega]+HoldForm[x]).HoldForm[x]] Albeit cumbersome, it does the trick. To avoid it one can use @march or @MariusLadegårdMeyer solutions! Thank you.

5

There will be more clever answers from people who better understand Mathematica's order of evaluation and how to use Hold and such, and so I can't answer your question in exactly the way that you've phrased it, but here's how I go about doing these types of things. First, instead of declaring the values of x, omega, and R as you've done, make a list of ...

3

I think $PreRead may be your only hope (but see below). You can set it up with$PreRead = (# /. RowBox@{"PermanentRespect", "[", expr_, "]"} | RowBox@{"PermanentRespect", "@", expr_} | RowBox@{expr_, "//", "PermanentRespect"} :> RowBox@{"RawBoxes", "[", MakeBoxes@expr, "]"} ) &; LoseRespect[expr_] := expr /. RawBoxes -> ToExpression ...

5

Defer is a special head that behaves like Hold, but it has an additional rule for output: it disappears from the output box expression. There is nothing special about how it is handled as input -- all the "magic" takes place during output formatting. To emulate this process, simply apply a ToBoxes ToExpression pair: r = Defer[Integrate][Cos[x], x]; r // ...

7

As suggested in the comments by both me and Jens r = Defer[Integrate][Cos[x], x]; r /. Defer -> Identity Sin[x]

0

z = 10.^-15; N[z/(z/2 + 1), 100] log1pcontracted[z_,n:_Integer?Positive:2]:=(2z)/(2+z+((-10-5 z) z^2)/(60+z (60+11 z)+Fold[#2[[1]]/(#2[[2]]+#1)&,0,Transpose[{Table[-(324+m (1224+m (1812+m (1312+m (464+64 m)))))z^4/(5+4m),{m,n,0,-1}],Table[(1260+z (1260+154 z)+m (2288+z (2288+284 z)+m (1344+z (1344+168 z)+m (256+z (256+32 z)))))/(5+4m),{m,n,0,-1}]}]])); ...

2

My quick-n-dirty take on this: mergef = Module[{ds = Symbol /@ ("s" <> ToString@# & /@ Range@100), fs = ##, rf}, rf = Plus@Through[fs[Sequence @@ ds]]; Function @@ {Take[ds, Max[Position[ds, #] & /@ Cases[rf, Alternatives @@ ds, Infinity]]], rf}] &; (* do some stuff *) f = # &; g = Function[{a}, a^2]; h = (-2 ...

4

Reading your question and comments again, and assuming that none of your pure functions contain SlotSequence, I think maybe this will work for you: combine[expr_] := Max[ Cases[expr, Slot[n_] :> n, {-2}], Cases[expr, Verbatim[Function][x_List, __] :> Length@Unevaluated@x, {1}] ] // Function @@ {Through[expr @@ Array[Slot, #]]} & Test: f ...

8

You must remember that Unevaluated only "works" when it is the explicit head of an expression. In the non-working format the structure looks like: TreeForm @ HoldForm[lispify /@ Unevaluated /@ {arg1, arg2}] Note that Unevaluated does not surround arg1 and arg2 therefore they evaluate prematurely. Now compare the working structure: TreeForm @ ...

2

Could this work for you? f = # &; g = #^2 &; h = (-2 #1 + #3) &; totalfunc = (f + g + h) /. f_[fl__Function] :> Module[{arg = {fl} /. Function -> List, int}, int = f @@ arg; Function @@ (Evaluate[int]) ] totalfunc[6, 3, 3] ==> 33 The replacement rule is rather general. EDIT: In the case you ...

2

I don't believe you can do it with Through[ ]. The following works: f = {#1, #2} &; g = Sin[{#1, #2}/#3] &; h = Cos[{#1 + #2, #3 + #4 + #5}] &; expr = Evaluate[(Plus @@ (First /@ {f, g, h}))] & (* {Cos[#1 + #2] + Sin[#1/#3] + #1, Cos[#3 + #4 + #5] + Sin[#2/#3] + #2} & *) expr[a, b, c, d, e] (* {a + Cos[a + b] + Sin[a/c], b + Cos[c + d ...

2

Is this near to what you are looking for? ClearAll[f, g, h] f1[s__] := Total@Take[{s}, 3]; f2[s__] := Times @@ Take[{s}, 2]; f3[s__] := {s}[[1]] {s}[[3]] - {s}[[2]] {s}[[4]]; expr = Through[(f + g + h)[##]] & w = expr /. {f -> f1, g -> f2, h -> f3}; w[5, 6, 7, 8] (* 35 *)

1

Very similar to your other question. Replace the following definitions in your code: u[t_] := 1/ b2[t]*(-(a21[t]*x1[t] + a22[t]*x3'[t]) + r2dot[t] - c (x3'[t] - rdot[t]) - k*2/Pi*ArcTan[((x3'[t] - rdot[t]) + c (x3[t] - r[t]))]); sol = First[NDSolve[ {x1'[t] == a11[t]*x1[t] + a12[t]*x3'[t] + a13[t]*x3[t] + b1[t]*u[t], x3''[t] == ...

Top 50 recent answers are included