# Mathematica derivation Question

• Mathematica
I'm trying to work through a derivation and am getting some funny results. For example, when trying to compare some expressions, Mathematica was telling me they weren't equal, and when I worked them out by hand, I new they were equal. I then tried something like this:

ExpandAll[x] === ExpandAll[x]

x === ExpandAll[x]

Returns

True

False

The second shows me that the form an expression determines If mathematica can perform some computations. But how do I ever trust this?? Now I'm tyring to compare two vectors, that I'm almost positive are the same, but mathematica returns: False. My trick of ExpandAll doesn't work in this case either.

can someone explain why this occurs, or how to reliably compare two expressions of different forms?

Thanks

In:= FullForm[x]
Out= x

In:= ExpandAll[x] === ExpandAll[x]
Out= True

In:= x === ExpandAll[x]
Out= True

Can I be positive that you have not somehow assigned some value to x in one of those and not in the other? Can I be positive that there is not some hidden bug in the version of Mathematica you are using that is different from some hidden bug in the version of Mathematica I am using? Can I see the two vectors that you are almost positive are the same so I could try to verify whether they are the same or not?

no.

=== imposes a very strict standard on comparison, a much higher standard than == imposes. The documentation for Mathematica almost always doesn't provide the level of detail to be positive what the actual semantics are.

I have determined that the problem is coming from the fact that I have some roots that mathematica doesn't know what to do with, without some further assumptions. For instance, as a test I've tried this:

o = {x , y};

n = Sqrt[{x^2, y^2}];

o === n

False

I now have a follow up question - Can I specify an assumption globally? Such as x,y >0 so that mathematica will simplify the squareroots?

Some users think Mathematica Assumptions are much more powerful than they actually are. Assumptions only "put a note up on the wall." A few Mathematica functions, like Simplify and Integrate, will look at the notes left on the wall while most functions will ignore these.

In:= \$Assumptions = {x > 0, y > 0};
o = {x, y};
n = Sqrt[{x^2, y^2}];
o === n

Out= False

In:= o == n

Out= {x, y} == {Sqrt[x^2], Sqrt[y^2]}

In:= n = Simplify[Sqrt[{x^2, y^2}]];
o === n

Out= True

In:= o == n

Out= True

Last edited:
• 1 person

I'm just wondering if you can explain why you type := before your input in In and In ? (I'm a total mathematica Newbie if you haven't figured that out yet.

Also, I have another question. I have a big long expression, and I would like to substitue in something, for example v = (1/1+x), and rearange my expression so that it is expressed in v as often as possible. I can't figure out how to do something like this exactly. The closest thing I found was the Eliminate function, but what if the expression does not completely remove the variable? I would just like to express it like this to simplify the expression.

I'm just wondering if you can explain why you type := before your input in In and In ? (I'm a total mathematica Newbie if you haven't figured that out yet.

Excellent question. Sometimes when people post questions or answers and include Mathematica code it isn't clear exactly what they have done.

What I try to do is start with a fresh notebook, hide nothing about what I've done, evaluate every step in order and then literally scrape-n-paste the contents of the entire notebook into an answer. When Mathematica evaluates an input cell it reformats the result with an In[nn]:=MyInputStuff and then displays the result as Out[nn]=TheResult. If you try entering an expression followed by <shift><enter> you should see this in your notebook.

So you should be able to read any answer I provide and be able to identify the inputs and outputs and even scrape those inputs and put them into your own notebook to use.

Far too often people seem to spend all their time trying to use desktop publishing to turn their Mathematica code into fancy formatted graphics. When they are done all this has to be reverse engineered to get it back into Mathematica and then try to figure out what they really did and why it didn't work.

Also, I have another question. I have a big long expression, and I would like to substitue in something, for example v = (1/1+x), and rearange my expression so that it is expressed in v as often as possible. I can't figure out how to do something like this exactly. The closest thing I found was the Eliminate function, but what if the expression does not completely remove the variable? I would just like to express it like this to simplify the expression.

A large part of Mathematica depends on patterns and substitutions. This can be a very powerful feature. Unfortunately it is also probably one of the most aggravating and difficult to use and misunderstood part of Mathematica.

You can try doing substitution with something like this

BigLongExpression /.(1/(1+x))->v

That will attempt to find all the 1/(1+x) in your expression and replace it with v.

Unfortunately in many cases this will not accomplish what you think makes perfect sense. Then you can start trying to learn how to wrestle Mathematica into submission.

I strongly urge every new Mathematica user to get at least a few books on how to use Mathematica and read them cover to cover. Some older books are available from book sellers for very small amounts of money. For something as complicated as Mathematica I think it is very difficult for someone to "figure it all out for themselves without using any good information."

Thanks Again!

I tried using the /. (1/(1+x)) -> v idea before, and it will substitute as long as there is a (1/(1+x)) exactly in this form. But, for instance, if there is something like -2/(2+2x), it won't substitute as -2v. I'd like mathematica to do some basic rearrangements when possible, but from what you're saying, that's not as trivial as I was hoping...

I got a book called ''Mathematica Cookbook'' (published by O'Reily) that looks pretty good, and I have been leafing through it. The problem is I don't have time to read this before starting my project. I have to begin it right now. I would much prefer to read this book all the way through before beginning. But I guess that's how it goes sometimes.

I tried using the /. (1/(1+x)) -> v idea before, and it will substitute as long as there is a (1/(1+x)) exactly in this form. But, for instance, if there is something like -2/(2+2x), it won't substitute as -2v. I'd like mathematica to do some basic rearrangements when possible, but from what you're saying, that's not as trivial as I was hoping...

In many ways Mathematica does not have "mathematical maturity", which means looking at something like any modestly experienced person in math would look at math and do the sensible thing. Mathematica has what i think is called "structural matching." That means the structure has to exactly match.

I believe your example isn't quite correct, I think you may have meant -2/(1+x) to result in -2v. Being extremely careful to be exactly correct about all the details will be very important in getting Mathematica and pattern matching to work for you.

There was an article published in the Mathematica Journal more than a decade ago which showed how it was possible to have a much more powerful "semantic substitution" which is much more like the common sense mathematics that we understand. That appears to have been completely ignored by Wolfram Inc and the author appears to have given up on the idea completely. But if something like this were available it might be far less frustrating to do pattern matching.

I tried many years ago to get support for the idea of giving a description of what I wanted an expression to mostly match and have it try to hammer as much as possible into that form and then tack on any remaining bits at the end. For example, I had a huge expression that I was sure was almost exactly a perfect square, plus or minus a few extra bits. No matter how hard I tried I could not force Mathematica to find (muchSmaller)^2+aFewBits. I had zero success trying to get anyone interested in implementing a feature like this.

Hint: When pattern matching just refuses to work try looking at the FullForm of an expression. That can sometimes give you the clues needed to get your request into a form that Mathematica will accept.

I got a book called ''Mathematica Cookbook'' (published by O'Reily) that looks pretty good, and I have been leafing through it. The problem is I don't have time to read this before starting my project. I have to begin it right now. I would much prefer to read this book all the way through before beginning. But I guess that's how it goes sometimes.

Reading the whole cookbook before starting your project is probably too much to expect. But if you can pick out a handful of chapters that are directly related to what you want to do then you might save time by reading those first.

I think you may have meant -2/(1+x) to result in -2v.

Ha! You're right. Maybe I should stop blaming mathematica, when evidence points to human error. :)

There is more than enough blame to go around. The task is to figure out
1: Exactly what the problem is.
2: Exactly why the problem is.
3: Exactly whose fault the problem is.
4: Exactly what can be done to get the work done in spite of all this.

For substitution you can do things like this:

In:= -3/(1 + x) + a/(1 + x) /. c_/(1 + x) :> c*v

Out= -3 v + a v

But there is substantial magic in those two lines and even when you think you understand why that did what it did there are still deeper levels of magic to figure out how to do the next thing in Mathematica that doesn't work.

It might be a challenge for you to figure out why this one doesn't work.

In:= -3/(1 + x) + 4/(1 + x) /. c_/(1 + x) :> c*v

Out= 1/(1 + x)

I suspect there is an unending stream of "OK, you have finally figured out why all the previous ones didn't work and what to do about each one to make it work... Now see if you can figure out this next one!"

If someone could write a brilliantly clear text that would explain the Mathematica evaluation process all the way down to the bottom so that the reader could finally really understand that the underlying mechanics of Mathematica is very very different from what it looks like then maybe we could all become far far better at this.

Last edited:
• 1 person