Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.5K Views
|
5 Replies
|
1 Total Like
View groups...
Share
Share this post:

How to simplify Dot expressions in Mathematica?

Posted 2 years ago

Hi everyone,

I'm trying to simplify an expression involving the Dot operation in Mathematica, but I'm having some trouble getting the desired output. For example, consider the following code:

n = 3;
X = Array[x, n];
Y = Array[y, n];
f[x_, y_] = (x - y) . (x - y);
expr = D[f[X, Y], {X}];

The resulting expression ·expr· is

{2 (x[1] - y[1]), 2 (x[2] - y[2]), 2 (x[3] - y[3])}

Now, I want to obtain a result in vector form as 2 (X - Y). If there any way to do so?

If I try

 expr = D[f[x, y], {x}];

Then, I obtained an expression involves the Dot operation:

1 . (x - y) + (x - y) . 1

which is closed to my desired result, but I want to merge the two terms involving (x - y) and obtain the simplified expression 2 (x - y). Using Simplify and FullSimplify don't seem to work in this case.

Is there a way to get my desired expression by simplfying the Dot expression or representing the result in vector form? Thanks in advance for your help!

POSTED BY: Yi-Shuai Niu
5 Replies

Yes, I suppose you can define your own calculus with symbolic vectors and matrices. I wonder if anyone has already done it.

POSTED BY: Gianluca Gorni

I am afraid that the operator D is not designed to do that kind of manipulation. It takes derivatives only with respect to (real or) complex variables, not with respect to symbolic vectors. The 1 you see in the output

1 . (x - y) + (x - y) . 1

is really the one-dimensional number 1, not an identity matrix. The expression does not simplify because Mathematica does not know what to do with it, it does not make much sense. The Dot operator in general is not commutative.

One thing you can do is check that the result agrees with your desired formula:

n = 3;
X = Array[x, n];
Y = Array[y, n];
f[x_, y_] = (x - y) . (x - y);
expr = D[f[X, Y], {X}];
expr == 2 (X - Y)

The answer is True.

POSTED BY: Gianluca Gorni
Posted 2 years ago

One thing you can do is check that the result agrees with your desired formula

You are right, we can always check it, but we have to guess a result first, which is unbearable things if the expression is too complicated...

As you said, 1 is not clearly defined, so mathematica cannot simplify the Dot expression. Moreover, It is very confusing to compute a gradient of the quadratic form $x^{\top}Ax$ by just doing

D[x . (A . x), x]

to get

1 . A . x + x . A . 1

Is there a way to define abstract vector x and matrix A to compute derivative correctly?

POSTED BY: Yi-Shuai Niu

You probably have misunderstood what Array does. Try this:

Clear[x, y]
n = 3;
xVector = Array[x, n];
yVector = Array[y, n];
f[x_, y_] = (x - y) . (x - y);
D[f[xVector, yVector], {xVector}]
POSTED BY: Gianluca Gorni
Posted 2 years ago

Thank you for your response. You are right! It's my mistake.

However, I have a follow-up question. Although the output of the D function you provided is correct, I was wondering if there is a way to represent the result in terms of vectors x and y, such as 2(x-y), rather than showing each element separately as

{2 (x[1] - y[1]), 2 (x[2] - y[2]), 2 (x[3] - y[3])}

Representing the results in vector form is very important to me as I will deal with more complex expressions. Thank you again for your time and assistance.

BTW: I've modified my question to match more precisely my expected result.

POSTED BY: Yi-Shuai Niu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard