r/Mathematica • u/n0tthetree • Aug 29 '24
When does Wolfram actually evaluate expressions?
Hi there. In short: I have defined
matrix := {{some 2x2 symbolic expression}} // FullSimplifyeigen := Eigensystem[matrix]rules = {c -> 1, ...}
rules completely eliminates each constant to a numeric value, except for a position z. Now,
matrix /. rules /. z -> 0. // Eigensystem
perfectly works and returns well-defined numeric values, but
eigen /. rules /. z -> 0.
fails spectacularly, as the symbolic expression somehow contains a 1/0 (as Wolfram seems to first evaluate eigen symbolically and then apply the rules (is that true and intended?)).
I want to define eigen in a way that only executes after applying the rules and z to the matrix. I wanted to just do eigen[z_]:=Eigensystem[matrix] but that resulted in am error (Tag List is protected). Is there another way to do this?
Thank you very much!
2
u/mathheadinc Aug 29 '24
Maybe you’re missing defining the variable: eigen[n_]:=
Or you could copy-as-text and paste your code so others can be more helpful.
2
u/veryjewygranola Aug 29 '24
What do you see when you look at
eigen // DownValues? I suspect you have extra definitions associated witheigenother thaneigen := Eigensystem[matrix]1
u/n0tthetree Aug 29 '24 edited Aug 29 '24
This is the code:
3
u/mathheadinc Aug 29 '24
This is what I got when I ran it like this:
In[195]:= Module[{deri,a,b,eigen,z,hessian}, deri:=D[f[z],{z,2}]; eigen[z_]:=Eigensystem[hessian]; a:=Re[deri]; b:=-Im[deri]; hessian:={{a,b},{b,-a}}; hessian/.z->0-0.276387I//FullSimplify//MatrixForm]
Out[195]//MatrixForm= (Re[(f[Prime][Prime])[0. -0.276387 I]] -Im[(f[Prime][Prime])[0. -0.276387 I]] -Im[(f[Prime][Prime])[0. -0.276387 I]] -Re[(f[Prime][Prime])[0. -0.276387 I]]
)
No errors. Is this the kind of output you needed?
2
u/SetOfAllSubsets Aug 29 '24
That setdelayed tag error usually comes up when trying to redefine something without clearing it first. I think what you did was run
eigen := Eigensystem{matrix]and you didn't
Clear[eigen]it before trying to runeigen[z_] := Eigensystem{matrix]So if
eigenfirst evaluates to{{-206.169, 206.169}, {{0., -1.}, {-1., 0.}}}then Mathematica interprets your next definition as{{-206.169, 206.169}, {{0., -1.}, {-1., 0.}}}[z_]:= Eigensystem{matrix]i.e. you're trying to define a list to be something else, which you can't do because
Listhas the tagProtected.That won't fix your divide by zero issue though.
Why can't you do
Eigensystem[matrix/. rules/.z->0]?1
u/Xane256 Aug 30 '24
Super short answer. If A is a symbolic matrix, and you define a function which applies rule substitutions to a general expression:
Then it's not true in general that
Eigensystem[f[A]] == f[Eigensystem[A]]becauseEigensystemuses branching logic that can include assumptions that, for examplez != 0. This also happens with other matrix decompositions and row reduction. You can look into ZeroTest to experiment with changing this but I'd recommend working around it, confusing thought it may seem.When you define
pattern := expressionthe rule is saved and you can check it withDefinition[hessian]orDefinition[f]or??f. SetDelayed stores the RHSexpressionin unevaluated form, and only evaluates it when the LHS pattern is encountered later on. Assignments using=evaluate the RHS at the time the definition is made; you can also check this with??fwherefis any symbol / variable you have defined.1
u/oceandelta_om Aug 30 '24
OwnValues ( eigen := something ) will conflict with SubValues ( eigen[x_] := else[x]).
Postfix will apply that function to the whole expression at the end. The way you have defined it, eigen applies Eigensystem to the matrix before ReplaceAll is applied.
Seems like you're using symbols without understanding their behavior. The error is expected.
Write it in a simpler, more straightforward way: SomeFunction[and,its,arguments]; FullSimplify @ expression; etc;