Tag Info

New answers tagged

2

My one shot at answering this question: Attributes[convert] = {HoldFirst}; convert[def_Symbol?ValueQ] := With[{old = def, pats = Quiet[Sequence @@ Cases[Variables @ def, s_Symbol :> s_]]}, ClearAll[def]; def[pats] := old; ] Test: positionPayload = 2. x + 3. t^2; convert[positionPayload] ?? positionPayload Global`positionPayload ...


3

You might try something like this, which makes pure functions, which means the variables used int the expression to converted only have to be clear at time expToF is called. expToF[exp_, vars : {_Symbol ..}] := With[{body = exp /. Thread[Rule[vars, Slot /@ Range @ Length[vars]]]}, Function[body]] Clear[x,t] f = expToF[2. x + 3. t^2, {x, t}]; f[x,t] ...


4

Not that I would recommend this, but anyway: positionPayload = 2. x + 3. t^2; variableList = Variables[positionPayload]; positionPayload = Function[Evaluate@variableList, Evaluate@positionPayload]; positionPayload[q, r] (* 3. q^2 + 2. r *) Edit positionPayload = 2. x[1] + 3. t[3]^2; variableList = Variables[positionPayload]; ul = ...


2

positionPayload = 2. x + 3. t^2 (* 3. t^2 + 2. x *) variableList = DeleteDuplicates[Variables[positionPayload]] (* {t, x} *) temp = positionPayload; positionPayload =. Evaluate[positionPayload @@ (Pattern[#, Blank[]] & /@ variableList)] := Evaluate@temp Definition@positionPayload (* positionPayload[t_, x_] := 3. t^2 + 2. x *) positionPayload[q, r] (* ...


3

I think this is what you're trying to get at. Say you're solving an equation that generates Root expressions: solution = Solve[x^5 - x^4 + 13 == 0, x] This gives a list of rules with a number of Root expression solutions. Let's just take a look at the first one, for simplicity: firstroot = x /. First[solution] (* gives: Root[13 - #1^4 + #1^5 & , 1, ...


8

The problem is that the function name f is substituted into Function (&) which is HoldAll. This means f[t] will not be evaluated until the Function is evaluated, such as in the OP's example problem[one][17]. So the trick is to evaluate the integrand before inserting it into the Function. Here is one way. one[x_] := 1 (*the argument*) problem[f_] := ...


1

I interpret this question as asking how define a function that works like the built-in functions Part, AppendTo, and PrependTo; i.e., a function that performs non-standard argument evaluation because it has been given one of the attributes from the Hold family of attributes. Normally, in what is referred to as standard evaluation, all the actual arguments ...


1

I think that you are looking for something analogous to various modify-in-place functions such as AddTo, AppendTo, PrependTo, AssociateTo, SubtractFrom, Increment, Decrement, but for general function application. Being aware of Apply and bearing in mind the difference in meaning between simply "function application" and the specific operation of Apply I ...


1

deriv[f_[x_]] := Inactivate[ ToExpression@StringJoin[ToString[f], ToString[x]] = D[f[x], x], Set | D] Then: deriv[kr[x]] outputs Inactive[Set][kryx, Inactive[D][kr[yx], yx]] which displays as kryx = D[kr[yx], yx]


1

You need anonymous functions. The way it's written currently will replace every instance of x on the right hand side with the number you provide as the argument for collatz. What you want is something which can act on the value produced by NestWhileList on each iteration. So use Clear[collatz] collatz[x_] :=NestWhileList[If[Mod[#, 2] == 0, #/2, #*3 + ...


3

x is just your starting value for the iteration. The function to apply, and the stopping condition, should both be applied to the current value obtained by the iteration, and are probably best expressed using pure functions: collatz[x_] := NestWhileList[If[Mod[#, 2] == 0, #/2, #*3 + 1] &, x, # != 1 &] collatz[5] (* Out: {5, 16, 8, 4, 2, 1} *) ...


2

It seems to be that all you need is SetDelayed (:=) instead of Set (=): Derivative[1, 0, 0, 0][f][a_, b_, c_, d_] := f[a + 1, b, c, d] Now let us evaluate some derivatives: D[f[x, y, z, p], {x, 1}] (* with respect to that first argument *) D[f[x, y, z, p], {y, 1}] (* with respect to any other argument *) (*Out: f[1 + x, y, z, p] Derivative[0, 1, 0, ...


-1

This should work: Replace[{484/45, -16 EulerGamma/3, -8 Log[2], PolyGamma[0, 1/Sqrt[2]], (48/5 + 2 I/5) Sqrt[2] Log[1 + Sqrt[2]]}, _?ExactNumberQ -> 1, 2] (* {1, EulerGamma, Log[2], PolyGamma[1, 1/Sqrt[2]], Sqrt[2] Log[1 + Sqrt[2]]}*)


4

Caveat Instead of answering your question I am going to try to convince you not to do what you are describing. While it is true that Mathematica is highly configurable in many areas there are nevertheless paradigms and principles that are best heeded. You did not give an example of where or why you feel a usage message is needed so it is difficult to ...


1

rcollyer provides the literal answer to your question in his answer. Another way to do it is simply to give f the HoldFirst attribute and make it take an additional first argument, namely the symbol you want to link the message to, i.e. v. Then you can do v::usage="bla" and v=value inside the definition of f, and just call it like f[v,...] instead of ...


4

As follows, are three different methods to answer your question. They each have there merits and flaws. The first is a literal attempt to do as you ask, but it requires modifying the behavior of a System` function to do it. The second method deviates from what you ask by using a different binary operator for the assignment, but still links a message to the ...


1

Although I urge you to consider a method like the one below as I think it will ultimately make your coding experience easier, for the specific example in hand you can prevent the unwanted evaluation by writing speakers inside AdjustOneSpeaker where it is held (by HoldAll). Note also that you need to map over the Keys of the association, not its values. ...


0

gs = Flatten @ Table[With[{m = m, n = n}, Sin[m #/n^2] &], {m, 2}, {n, 2}]; {Sin[#1/1^2] & , Sin[#1/2^2] & , Sin[(2*#1)/1^2] & , Sin[(2*#1)/2^2] &} f[x_, a_] = a Through[gs[x]]; f[u, b] {b*Sin[u], b*Sin[u/4], b*Sin[2*u], b*Sin[u/2]}


4

You can do the whole thing numerically by replacing the Heaviside function with a numerical version. For example: nHeaviside[x_] := If[x < 0, 0, 1] will work. You can choose which value it will assume at zero by changing the condition to x <= 0. Alterantively, the function Unitstep does a similar task: nHeaviside2[x_] := UnitStep[x] (Guess who ...


8

The answer to the question depends upon what exactly should be called as "graphics primitive". In this answer from the practical point of view I define it as a container which can be found inside of Graphics or Graphics3D, which draws something and is not a graphical directive or Dynamic wrapper. This definition differs from the usual meaning but covers all ...


1

Just maybe this is related to what you want? min[a_, b_] := Piecewise[{{a, #}, {b, ! #}}] & @ Reduce[a < b] min[x__] := Fold[min, {x}] // PiecewiseExpand min[-x^2, -(x + 1)^2] Piecewise[{{-x^2, x < -1/2}, {-(1 + x)^2, x >= -1/2}}, 0] J.M. just pointed out in a comment that Min already works this way when using PiecewiseExpand: ...


2

To be honest, I was quite surprised that Mathematica finds an analytical solution when you just enter the problem in naively: Area[RegionIntersection[Disk[{l, 0}, l], Disk[{0, l}, l], Disk[{l/2, l/2}, l/2]]] On Mathematica 10.1 I don't get the expression with complex terms that Fernando got above, I get the purely real ConditionalExpression[-(1/4) ...


7

Since there seems no other simple answer at this time I propose the approach of rendering a Graphics expression and seeing if it has errors. By definition this will pass both primitives and directives, as well as inert expressions such as {}. I hope it nevertheless serves some purpose. I rasterize the graphic and look for the tell-tale pink warning color. ...


17

Edit: It was pointed out that the original form is not bullet-proof, e.g. GraphicsPrimitiveQ /@ {InputNotebook, Unique, Sequence} all returned True. However, when looking upon this answer about generating new graphics primitives, a superior answer came to light: Clear[GraphicsPrimitiveQ]; GraphicsPrimitiveQ[s_Symbol | (s_)[___]] := 0 < ...


1

The key here is to understand the Apply operator. There we want to apply f at level 1 to the list. Therefore, like Guess who it is suggested we can first apply the function f[x, y, z, ##] where ## gets filled in from your list because of the the @@@. Then Total adds up all the functions. Total[f[x, y, z, ##] & @@@ list]


2

For exactly the reasons mentioned in another answer, (scale PDF[NormalDistribution[]])[0.5] doesn't work since the Head of the expression is Times rather than Function. This suggests that we should replace the innards of the Function. Something like scale = 10; f = PDF[NormalDistribution[]] /. (Function[b_] :> Function[scale b]); Then f @ 0.5 results ...


3

As I see it, this behavior does not depend on Hold properties. In fact, PDF[NormalDistribution[]] evaluates to a Function, so you can apply it to arguments using []: Head[PDF[NormalDistribution[]]] (* Function *) On the other hand, (scale PDF[NormalDistribution[]]) does not evaluate to an expression with head Function; in fact, it evaluates to an ...


3

There will be an infinite number of functions. You could approach as follows: f[a_, b_, c_, d_, x_] := a*Log[b*x + c] + d then FindInstance[{f[a, b, c, d, 0] == 1, f[a, b, c, d, 80] == 0.5}, {a, b, c, d}, Reals] this yields: {{a -> 7.95441, b -> -0.098772, c -> 1297/10, d -> -(377/10)}} or sol = First[Quiet@Solve[{f[a, b, c, d, 0] == ...


3

You need to create a pure Function and Map it over the first level of tstlst like so: LinearModelFit[#, x, x] & /@ tstlst Now tstlst can have as many sets as you like without you having to know how many in advance.


5

Here is another possible solution which doesn't require you to use Simplify: Clear[f, a, g, r] f[arg_] := a - I arg Conjugate[g[r_]] ^:= g[r] Conjugate[f[g[r]]] (* ==> Conjugate[a] + I g[r] *) This uses UpSetDelayed to make the desired assumption part of the definitions associated with g.



Top 50 recent answers are included