quaternion renormalization
$begingroup$
I am trying to create 4 symbolic equations that correspond to the 4 elements of a normalized quaternion. The equation is for quaternion multiplication
delta q = 0.5*q*[0, omega]^T
and then I normalize the output. My solution is:
import sympy as sy
q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n = sy.symbols("q_r q_i q_j q_k
w_ib_l w_ib_m w_ib_n")
q1 = [q_r, q_i, q_j, q_k]
q2 = [0, w_ib_l, w_ib_m, w_ib_n]
def quat_equ(q_l,q_w):
q_r_d = 0.5 * (q_l[0] * q_w[0] - q_l[1] * q_w[1] + q_l[2] * q_w[2] +
q_l[3] * q_w[3])
q_i_d = 0.5 * (q_l[0] * q_w[1] + q_l[1] * q_w[0] + q_l[2] * q_w[3] -
q_l[3] * q_w[2])
q_j_d = 0.5 * (q_l[0] * q_w[2] + q_l[2] * q_w[0] + q_l[3] * q_w[1] -
q_l[1] * q_w[3])
q_k_d = 0.5 * (q_l[0] * q_w[3] + q_l[3] * q_w[0] + q_l[1] * q_w[2] -
q_l[2] * q_w[1])
for i in range(1):
q_r_d *= 1 / sy.sqrt(q_r_d**2 + q_i_d**2 + q_j_d**2 + q_k_d**2)
q_i_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_j_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_k_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
return [q_r_d, q_i_d, q_j_d, q_k_d]
quat0 = quat_equ(q1, q2)[0]
quat1 = quat_equ(q1, q2)[1]
quat2 = quat_equ(q1, q2)[2]
quat3 = quat_equ(q1, q2)[3]
qq = [1., 0., 0., 0., 1., 1., 1.]
var = [q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n]
q0 = sy.Subs(quat0, var, qq).doit()
q1 = sy.Subs(quat1, var, qq).doit()
q2 = sy.Subs(quat2, var, qq).doit()
q3 = sy.Subs(quat3, var, qq).doit()
When I try to re-normalize the quaternion more than 2 times this method becomes very slow. The symbolic equation also becomes massive because the re-normalization adds on basically 4 extra symbolic equations, which slows down later processes a lot. I couldn't think of a better method to create a normalized quaternion that 1) initializes faster and 2) is a smaller symbolic equation. I don't use symbolics in python much so most of Sympy is new to me.
python symbolic-math sympy
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I am trying to create 4 symbolic equations that correspond to the 4 elements of a normalized quaternion. The equation is for quaternion multiplication
delta q = 0.5*q*[0, omega]^T
and then I normalize the output. My solution is:
import sympy as sy
q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n = sy.symbols("q_r q_i q_j q_k
w_ib_l w_ib_m w_ib_n")
q1 = [q_r, q_i, q_j, q_k]
q2 = [0, w_ib_l, w_ib_m, w_ib_n]
def quat_equ(q_l,q_w):
q_r_d = 0.5 * (q_l[0] * q_w[0] - q_l[1] * q_w[1] + q_l[2] * q_w[2] +
q_l[3] * q_w[3])
q_i_d = 0.5 * (q_l[0] * q_w[1] + q_l[1] * q_w[0] + q_l[2] * q_w[3] -
q_l[3] * q_w[2])
q_j_d = 0.5 * (q_l[0] * q_w[2] + q_l[2] * q_w[0] + q_l[3] * q_w[1] -
q_l[1] * q_w[3])
q_k_d = 0.5 * (q_l[0] * q_w[3] + q_l[3] * q_w[0] + q_l[1] * q_w[2] -
q_l[2] * q_w[1])
for i in range(1):
q_r_d *= 1 / sy.sqrt(q_r_d**2 + q_i_d**2 + q_j_d**2 + q_k_d**2)
q_i_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_j_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_k_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
return [q_r_d, q_i_d, q_j_d, q_k_d]
quat0 = quat_equ(q1, q2)[0]
quat1 = quat_equ(q1, q2)[1]
quat2 = quat_equ(q1, q2)[2]
quat3 = quat_equ(q1, q2)[3]
qq = [1., 0., 0., 0., 1., 1., 1.]
var = [q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n]
q0 = sy.Subs(quat0, var, qq).doit()
q1 = sy.Subs(quat1, var, qq).doit()
q2 = sy.Subs(quat2, var, qq).doit()
q3 = sy.Subs(quat3, var, qq).doit()
When I try to re-normalize the quaternion more than 2 times this method becomes very slow. The symbolic equation also becomes massive because the re-normalization adds on basically 4 extra symbolic equations, which slows down later processes a lot. I couldn't think of a better method to create a normalized quaternion that 1) initializes faster and 2) is a smaller symbolic equation. I don't use symbolics in python much so most of Sympy is new to me.
python symbolic-math sympy
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
1
$begingroup$
Why isfor i in range(1)written as a loop? It's indentation is wrong, which makes this broken code. (A good way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.)
$endgroup$
– 200_success
5 hours ago
$begingroup$
Note that MathJax is available for writing equations in your question.
$endgroup$
– 200_success
5 hours ago
$begingroup$
In addition to what 200_success alreay said about broken code, the symbol definition is also no valid Python code. Your code could also greatly benefit from some comments to give reviewers a few hints which parts you suspect or know to be slow.
$endgroup$
– Alex
3 hours ago
add a comment |
$begingroup$
I am trying to create 4 symbolic equations that correspond to the 4 elements of a normalized quaternion. The equation is for quaternion multiplication
delta q = 0.5*q*[0, omega]^T
and then I normalize the output. My solution is:
import sympy as sy
q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n = sy.symbols("q_r q_i q_j q_k
w_ib_l w_ib_m w_ib_n")
q1 = [q_r, q_i, q_j, q_k]
q2 = [0, w_ib_l, w_ib_m, w_ib_n]
def quat_equ(q_l,q_w):
q_r_d = 0.5 * (q_l[0] * q_w[0] - q_l[1] * q_w[1] + q_l[2] * q_w[2] +
q_l[3] * q_w[3])
q_i_d = 0.5 * (q_l[0] * q_w[1] + q_l[1] * q_w[0] + q_l[2] * q_w[3] -
q_l[3] * q_w[2])
q_j_d = 0.5 * (q_l[0] * q_w[2] + q_l[2] * q_w[0] + q_l[3] * q_w[1] -
q_l[1] * q_w[3])
q_k_d = 0.5 * (q_l[0] * q_w[3] + q_l[3] * q_w[0] + q_l[1] * q_w[2] -
q_l[2] * q_w[1])
for i in range(1):
q_r_d *= 1 / sy.sqrt(q_r_d**2 + q_i_d**2 + q_j_d**2 + q_k_d**2)
q_i_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_j_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_k_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
return [q_r_d, q_i_d, q_j_d, q_k_d]
quat0 = quat_equ(q1, q2)[0]
quat1 = quat_equ(q1, q2)[1]
quat2 = quat_equ(q1, q2)[2]
quat3 = quat_equ(q1, q2)[3]
qq = [1., 0., 0., 0., 1., 1., 1.]
var = [q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n]
q0 = sy.Subs(quat0, var, qq).doit()
q1 = sy.Subs(quat1, var, qq).doit()
q2 = sy.Subs(quat2, var, qq).doit()
q3 = sy.Subs(quat3, var, qq).doit()
When I try to re-normalize the quaternion more than 2 times this method becomes very slow. The symbolic equation also becomes massive because the re-normalization adds on basically 4 extra symbolic equations, which slows down later processes a lot. I couldn't think of a better method to create a normalized quaternion that 1) initializes faster and 2) is a smaller symbolic equation. I don't use symbolics in python much so most of Sympy is new to me.
python symbolic-math sympy
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I am trying to create 4 symbolic equations that correspond to the 4 elements of a normalized quaternion. The equation is for quaternion multiplication
delta q = 0.5*q*[0, omega]^T
and then I normalize the output. My solution is:
import sympy as sy
q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n = sy.symbols("q_r q_i q_j q_k
w_ib_l w_ib_m w_ib_n")
q1 = [q_r, q_i, q_j, q_k]
q2 = [0, w_ib_l, w_ib_m, w_ib_n]
def quat_equ(q_l,q_w):
q_r_d = 0.5 * (q_l[0] * q_w[0] - q_l[1] * q_w[1] + q_l[2] * q_w[2] +
q_l[3] * q_w[3])
q_i_d = 0.5 * (q_l[0] * q_w[1] + q_l[1] * q_w[0] + q_l[2] * q_w[3] -
q_l[3] * q_w[2])
q_j_d = 0.5 * (q_l[0] * q_w[2] + q_l[2] * q_w[0] + q_l[3] * q_w[1] -
q_l[1] * q_w[3])
q_k_d = 0.5 * (q_l[0] * q_w[3] + q_l[3] * q_w[0] + q_l[1] * q_w[2] -
q_l[2] * q_w[1])
for i in range(1):
q_r_d *= 1 / sy.sqrt(q_r_d**2 + q_i_d**2 + q_j_d**2 + q_k_d**2)
q_i_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_j_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
q_k_d *= 1 / sy.sqrt(q_r_d ** 2 + q_i_d ** 2 + q_j_d ** 2 + q_k_d ** 2)
return [q_r_d, q_i_d, q_j_d, q_k_d]
quat0 = quat_equ(q1, q2)[0]
quat1 = quat_equ(q1, q2)[1]
quat2 = quat_equ(q1, q2)[2]
quat3 = quat_equ(q1, q2)[3]
qq = [1., 0., 0., 0., 1., 1., 1.]
var = [q_r, q_i, q_j, q_k, w_ib_l, w_ib_m, w_ib_n]
q0 = sy.Subs(quat0, var, qq).doit()
q1 = sy.Subs(quat1, var, qq).doit()
q2 = sy.Subs(quat2, var, qq).doit()
q3 = sy.Subs(quat3, var, qq).doit()
When I try to re-normalize the quaternion more than 2 times this method becomes very slow. The symbolic equation also becomes massive because the re-normalization adds on basically 4 extra symbolic equations, which slows down later processes a lot. I couldn't think of a better method to create a normalized quaternion that 1) initializes faster and 2) is a smaller symbolic equation. I don't use symbolics in python much so most of Sympy is new to me.
python symbolic-math sympy
python symbolic-math sympy
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 6 hours ago
BotoBoto
111
111
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Boto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
$begingroup$
Why isfor i in range(1)written as a loop? It's indentation is wrong, which makes this broken code. (A good way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.)
$endgroup$
– 200_success
5 hours ago
$begingroup$
Note that MathJax is available for writing equations in your question.
$endgroup$
– 200_success
5 hours ago
$begingroup$
In addition to what 200_success alreay said about broken code, the symbol definition is also no valid Python code. Your code could also greatly benefit from some comments to give reviewers a few hints which parts you suspect or know to be slow.
$endgroup$
– Alex
3 hours ago
add a comment |
1
$begingroup$
Why isfor i in range(1)written as a loop? It's indentation is wrong, which makes this broken code. (A good way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.)
$endgroup$
– 200_success
5 hours ago
$begingroup$
Note that MathJax is available for writing equations in your question.
$endgroup$
– 200_success
5 hours ago
$begingroup$
In addition to what 200_success alreay said about broken code, the symbol definition is also no valid Python code. Your code could also greatly benefit from some comments to give reviewers a few hints which parts you suspect or know to be slow.
$endgroup$
– Alex
3 hours ago
1
1
$begingroup$
Why is
for i in range(1) written as a loop? It's indentation is wrong, which makes this broken code. (A good way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.)$endgroup$
– 200_success
5 hours ago
$begingroup$
Why is
for i in range(1) written as a loop? It's indentation is wrong, which makes this broken code. (A good way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.)$endgroup$
– 200_success
5 hours ago
$begingroup$
Note that MathJax is available for writing equations in your question.
$endgroup$
– 200_success
5 hours ago
$begingroup$
Note that MathJax is available for writing equations in your question.
$endgroup$
– 200_success
5 hours ago
$begingroup$
In addition to what 200_success alreay said about broken code, the symbol definition is also no valid Python code. Your code could also greatly benefit from some comments to give reviewers a few hints which parts you suspect or know to be slow.
$endgroup$
– Alex
3 hours ago
$begingroup$
In addition to what 200_success alreay said about broken code, the symbol definition is also no valid Python code. Your code could also greatly benefit from some comments to give reviewers a few hints which parts you suspect or know to be slow.
$endgroup$
– Alex
3 hours ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Boto is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216491%2fquaternion-renormalization%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Boto is a new contributor. Be nice, and check out our Code of Conduct.
Boto is a new contributor. Be nice, and check out our Code of Conduct.
Boto is a new contributor. Be nice, and check out our Code of Conduct.
Boto is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216491%2fquaternion-renormalization%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Why is
for i in range(1)written as a loop? It's indentation is wrong, which makes this broken code. (A good way to post code is to paste it into the question editor, highlight it, and press Ctrl-K to mark it as a code block.)$endgroup$
– 200_success
5 hours ago
$begingroup$
Note that MathJax is available for writing equations in your question.
$endgroup$
– 200_success
5 hours ago
$begingroup$
In addition to what 200_success alreay said about broken code, the symbol definition is also no valid Python code. Your code could also greatly benefit from some comments to give reviewers a few hints which parts you suspect or know to be slow.
$endgroup$
– Alex
3 hours ago