ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 20 Sep 2018 03:50:55 +0200Simplify vector vs. its normhttps://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/ In the following I compute the matrix `mrot` which shall rotate the vector `v` to the unit vector pointing into the x-direction. This does indeed work, except that the last printout for verification, `view(u*mrot)`, looks complicated, despite the fact that it should just be (1, 0, 0). How would I let sage reduce the enormous formulas for the vector components?
v1 = var('v1');
v2 = var('v2');
v3 = var('v3');
e1 = vector([1,0,0]);
v = vector([v1, v2, v3]);
u = v/v.norm();
#u = v;
view(["u=", u], sep=" ");
z=u.cross_product(e1);
#z=e1.cross_product(u);
view(["z=", z]);
c=u.dot_product(e1);
view(["c=",c]);
mat=Matrix( [[0, -z[3-1], z[2-1]], [z[3-1], 0, -z[1-1]], [-z[2-1], z[1-1], 0]] );
#view([mat, mat*mat]);
mrot=-(matrix.identity(3) + mat + (1/(1+c)) * mat*mat);
view(u*mrot);Sat, 25 Aug 2018 22:40:11 +0200https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/Answer by tmonteil for <p>In the following I compute the matrix <code>mrot</code> which shall rotate the vector <code>v</code> to the unit vector pointing into the x-direction. This does indeed work, except that the last printout for verification, <code>view(u*mrot)</code>, looks complicated, despite the fact that it should just be (1, 0, 0). How would I let sage reduce the enormous formulas for the vector components?</p>
<pre><code>v1 = var('v1');
v2 = var('v2');
v3 = var('v3');
e1 = vector([1,0,0]);
v = vector([v1, v2, v3]);
u = v/v.norm();
#u = v;
view(["u=", u], sep=" ");
z=u.cross_product(e1);
#z=e1.cross_product(u);
view(["z=", z]);
c=u.dot_product(e1);
view(["c=",c]);
mat=Matrix( [[0, -z[3-1], z[2-1]], [z[3-1], 0, -z[1-1]], [-z[2-1], z[1-1], 0]] );
#view([mat, mat*mat]);
mrot=-(matrix.identity(3) + mat + (1/(1+c)) * mat*mat);
view(u*mrot);
</code></pre>
https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?answer=43473#post-id-43473You can apply the `full_simplify()` method to every entry of the vector as follows:
sage: vector(e.full_simplify() for e in u*mrot)
(-(v1^2 - v2^2 - v3^2)/(v1^2 + v2^2 + v3^2), -2*v1*v2/(v1^2 + v2^2 + v3^2), -2*v1*v3/(v1^2 + v2^2 + v3^2))
It does not seem to look equal to \(1,0,0)` however :/Sun, 26 Aug 2018 16:37:09 +0200https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?answer=43473#post-id-43473Comment by Harald for <p>You can apply the <code>full_simplify()</code> method to every entry of the vector as follows:</p>
<pre><code>sage: vector(e.full_simplify() for e in u*mrot)
(-(v1^2 - v2^2 - v3^2)/(v1^2 + v2^2 + v3^2), -2*v1*v2/(v1^2 + v2^2 + v3^2), -2*v1*v3/(v1^2 + v2^2 + v3^2))
</code></pre>
<p>It does not seem to look equal to (1,0,0)` however :/</p>
https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?comment=43474#post-id-43474Well, this is because `full_simplify()` is not what is needed. I am working on a solution and it starts with not using `v.norm()`, but explicitly defining `T=v1^2 +v2^2 + v3^2` and using its `sqrt()`. This makes the result half understandable, but I need to find out more to get this solved.Sun, 26 Aug 2018 18:48:16 +0200https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?comment=43474#post-id-43474Answer by Harald for <p>In the following I compute the matrix <code>mrot</code> which shall rotate the vector <code>v</code> to the unit vector pointing into the x-direction. This does indeed work, except that the last printout for verification, <code>view(u*mrot)</code>, looks complicated, despite the fact that it should just be (1, 0, 0). How would I let sage reduce the enormous formulas for the vector components?</p>
<pre><code>v1 = var('v1');
v2 = var('v2');
v3 = var('v3');
e1 = vector([1,0,0]);
v = vector([v1, v2, v3]);
u = v/v.norm();
#u = v;
view(["u=", u], sep=" ");
z=u.cross_product(e1);
#z=e1.cross_product(u);
view(["z=", z]);
c=u.dot_product(e1);
view(["c=",c]);
mat=Matrix( [[0, -z[3-1], z[2-1]], [z[3-1], 0, -z[1-1]], [-z[2-1], z[1-1], 0]] );
#view([mat, mat*mat]);
mrot=-(matrix.identity(3) + mat + (1/(1+c)) * mat*mat);
view(u*mrot);
</code></pre>
https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?answer=43476#post-id-43476Here is how it works. In fact it is only slightly different from the above, but the two things that helped were `canonicalize_radical()` and mapping the simplify to the final vector.
v1 = var('v1');
v2 = var('v2');
v3 = var('v3');
e1 = vector([1,0,0]);
v = vector([v1, v2, v3]);
T=(v1^2 + v2^2 + v3^2);
u = v/sqrt(T);
z=u.cross_product(e1);
c=u.dot_product(e1);
mat=Matrix( [[0, -z[3-1], z[2-1]], [z[3-1], 0, -z[1-1]], [-z[2-1], z[1-1], 0]] );
mrot=(matrix.identity(3) + mat + (1/(1+c)) * mat*mat);
mrot = mrot*sqrt(T);
mrot = mrot.apply_map(lambda e: e.canonicalize_radical());
view(["mrot*sqrt(T)", mrot])
e = mrot/sqrt(T)*u;
e = e.apply_map(lambda e: e.full_simplify())
e
Multiplying `sqrt(T)` into `mrot` and taking it out again when multiplying with `u` is only for better readability.Sun, 26 Aug 2018 19:58:04 +0200https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?answer=43476#post-id-43476Answer by nbruin for <p>In the following I compute the matrix <code>mrot</code> which shall rotate the vector <code>v</code> to the unit vector pointing into the x-direction. This does indeed work, except that the last printout for verification, <code>view(u*mrot)</code>, looks complicated, despite the fact that it should just be (1, 0, 0). How would I let sage reduce the enormous formulas for the vector components?</p>
<pre><code>v1 = var('v1');
v2 = var('v2');
v3 = var('v3');
e1 = vector([1,0,0]);
v = vector([v1, v2, v3]);
u = v/v.norm();
#u = v;
view(["u=", u], sep=" ");
z=u.cross_product(e1);
#z=e1.cross_product(u);
view(["z=", z]);
c=u.dot_product(e1);
view(["c=",c]);
mat=Matrix( [[0, -z[3-1], z[2-1]], [z[3-1], 0, -z[1-1]], [-z[2-1], z[1-1], 0]] );
#view([mat, mat*mat]);
mrot=-(matrix.identity(3) + mat + (1/(1+c)) * mat*mat);
view(u*mrot);
</code></pre>
https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?answer=43477#post-id-43477You need to do
{{{
asume(v1,'real');
asume(v2,'real');
asume(v3,'real');
}}}
because some of the simplifications you need are not valid for complex numbers. You can just call "simplify_full" on the vector itself. It will apply to the entries.
It looks like you need to let mrot act on the left, though: mrot*u; or u*(mrot.T), and even then it looks like you get the vector (-1,0,0); so there may be a sign error somewhere or sage takes an unexpected branch cut somewhere.
Sun, 26 Aug 2018 21:20:56 +0200https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?answer=43477#post-id-43477Comment by Harald for <p>You need to do
{{{
asume(v1,'real');
asume(v2,'real');
asume(v3,'real');
}}}
because some of the simplifications you need are not valid for complex numbers. You can just call "simplify_full" on the vector itself. It will apply to the entries.</p>
<p>It looks like you need to let mrot act on the left, though: mrot<em>u; or u</em>(mrot.T), and even then it looks like you get the vector (-1,0,0); so there may be a sign error somewhere or sage takes an unexpected branch cut somewhere.</p>
https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?comment=43482#post-id-43482The minus sign in front of the definition of mrot is wrong in my original post, but given that originally I got a spaghetti plate full of formula, (-1, 0, 0) is great and easily fixed by removing the minus :-)Mon, 27 Aug 2018 07:37:16 +0200https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?comment=43482#post-id-43482Comment by slelievre for <p>You need to do
{{{
asume(v1,'real');
asume(v2,'real');
asume(v3,'real');
}}}
because some of the simplifications you need are not valid for complex numbers. You can just call "simplify_full" on the vector itself. It will apply to the entries.</p>
<p>It looks like you need to let mrot act on the left, though: mrot<em>u; or u</em>(mrot.T), and even then it looks like you get the vector (-1,0,0); so there may be a sign error somewhere or sage takes an unexpected branch cut somewhere.</p>
https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?comment=43706#post-id-43706Formatting note: note that
> mrot*u or u*(mrot.T)
will render as
> mrot*u or u*(mrot.T)
while
> `mrot*u` or `u*(mrot.T)`
will render as
> `mrot*u` or `u*(mrot.T)`Thu, 20 Sep 2018 03:50:55 +0200https://ask.sagemath.org/question/43470/simplify-vector-vs-its-norm/?comment=43706#post-id-43706