Sympy Simplify eliminate imaginary numbers
I'm trying to get the cosine similarity between convolved vectors. Because I'm using fast fourier transform, I am using complex numbers. In the calculation of the cosine similarity, the final value returned should be a real number. However, my output is including imaginary parts: 1.0*(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 + 5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
The imaginary portions should be zero (which they effectively are), but I can't get sympy to set the imaginary portions to zero so that I can get a real value as my output.
I've included the code that leads to the output. It's as pared down as I can accomplish.
# import statements
from sympy import *
from numpy import dot,array,random
# sympy initialization
a, b, c, d, e, f, g, h, i, j, k, l = symbols('a b c d e f g h i j k l')
# vector initialization
alpha = [a, b, c, d];
beta = [e, f, g, h];
gamma = [i, j, k, l];
# discrete fourier initialization (dft/idft)
W = [[1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j]];
WH = [[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j]];
# i/fft initialization, cosine similarity
def fft(a):
return dot(a,W)
def ifft(a):
return dot(a,WH)/4.0
def cosineSimilarity(a,b):
return dot(a,b)/(sqrt(dot(a,a)) * sqrt(dot(b,b)))
# x&y initialization
x = ifft(fft(alpha)*fft(beta)) + ifft(fft(alpha)*fft(gamma));
y = ifft(fft(alpha)*fft(beta)/fft(gamma)) +
ifft(fft(alpha)*fft(gamma)/fft(beta));
# determine cosine similarity between x&y
random.seed(39843)
current = random.rand(12)
mymap = list(zip(params,current))
print(simplify(diff(cosineSimilarity(x, y), a).subs(mymap)))
python fft sympy complex-numbers
add a comment |
I'm trying to get the cosine similarity between convolved vectors. Because I'm using fast fourier transform, I am using complex numbers. In the calculation of the cosine similarity, the final value returned should be a real number. However, my output is including imaginary parts: 1.0*(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 + 5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
The imaginary portions should be zero (which they effectively are), but I can't get sympy to set the imaginary portions to zero so that I can get a real value as my output.
I've included the code that leads to the output. It's as pared down as I can accomplish.
# import statements
from sympy import *
from numpy import dot,array,random
# sympy initialization
a, b, c, d, e, f, g, h, i, j, k, l = symbols('a b c d e f g h i j k l')
# vector initialization
alpha = [a, b, c, d];
beta = [e, f, g, h];
gamma = [i, j, k, l];
# discrete fourier initialization (dft/idft)
W = [[1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j]];
WH = [[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j]];
# i/fft initialization, cosine similarity
def fft(a):
return dot(a,W)
def ifft(a):
return dot(a,WH)/4.0
def cosineSimilarity(a,b):
return dot(a,b)/(sqrt(dot(a,a)) * sqrt(dot(b,b)))
# x&y initialization
x = ifft(fft(alpha)*fft(beta)) + ifft(fft(alpha)*fft(gamma));
y = ifft(fft(alpha)*fft(beta)/fft(gamma)) +
ifft(fft(alpha)*fft(gamma)/fft(beta));
# determine cosine similarity between x&y
random.seed(39843)
current = random.rand(12)
mymap = list(zip(params,current))
print(simplify(diff(cosineSimilarity(x, y), a).subs(mymap)))
python fft sympy complex-numbers
add a comment |
I'm trying to get the cosine similarity between convolved vectors. Because I'm using fast fourier transform, I am using complex numbers. In the calculation of the cosine similarity, the final value returned should be a real number. However, my output is including imaginary parts: 1.0*(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 + 5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
The imaginary portions should be zero (which they effectively are), but I can't get sympy to set the imaginary portions to zero so that I can get a real value as my output.
I've included the code that leads to the output. It's as pared down as I can accomplish.
# import statements
from sympy import *
from numpy import dot,array,random
# sympy initialization
a, b, c, d, e, f, g, h, i, j, k, l = symbols('a b c d e f g h i j k l')
# vector initialization
alpha = [a, b, c, d];
beta = [e, f, g, h];
gamma = [i, j, k, l];
# discrete fourier initialization (dft/idft)
W = [[1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j]];
WH = [[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j]];
# i/fft initialization, cosine similarity
def fft(a):
return dot(a,W)
def ifft(a):
return dot(a,WH)/4.0
def cosineSimilarity(a,b):
return dot(a,b)/(sqrt(dot(a,a)) * sqrt(dot(b,b)))
# x&y initialization
x = ifft(fft(alpha)*fft(beta)) + ifft(fft(alpha)*fft(gamma));
y = ifft(fft(alpha)*fft(beta)/fft(gamma)) +
ifft(fft(alpha)*fft(gamma)/fft(beta));
# determine cosine similarity between x&y
random.seed(39843)
current = random.rand(12)
mymap = list(zip(params,current))
print(simplify(diff(cosineSimilarity(x, y), a).subs(mymap)))
python fft sympy complex-numbers
I'm trying to get the cosine similarity between convolved vectors. Because I'm using fast fourier transform, I am using complex numbers. In the calculation of the cosine similarity, the final value returned should be a real number. However, my output is including imaginary parts: 1.0*(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 + 5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
The imaginary portions should be zero (which they effectively are), but I can't get sympy to set the imaginary portions to zero so that I can get a real value as my output.
I've included the code that leads to the output. It's as pared down as I can accomplish.
# import statements
from sympy import *
from numpy import dot,array,random
# sympy initialization
a, b, c, d, e, f, g, h, i, j, k, l = symbols('a b c d e f g h i j k l')
# vector initialization
alpha = [a, b, c, d];
beta = [e, f, g, h];
gamma = [i, j, k, l];
# discrete fourier initialization (dft/idft)
W = [[1, 1, 1, 1], [1, -1j, -1, 1j], [1, -1, 1, -1], [1, 1j, -1, -1j]];
WH = [[1, 1, 1, 1], [1, 1j, -1, -1j], [1, -1, 1, -1], [1, -1j, -1, 1j]];
# i/fft initialization, cosine similarity
def fft(a):
return dot(a,W)
def ifft(a):
return dot(a,WH)/4.0
def cosineSimilarity(a,b):
return dot(a,b)/(sqrt(dot(a,a)) * sqrt(dot(b,b)))
# x&y initialization
x = ifft(fft(alpha)*fft(beta)) + ifft(fft(alpha)*fft(gamma));
y = ifft(fft(alpha)*fft(beta)/fft(gamma)) +
ifft(fft(alpha)*fft(gamma)/fft(beta));
# determine cosine similarity between x&y
random.seed(39843)
current = random.rand(12)
mymap = list(zip(params,current))
print(simplify(diff(cosineSimilarity(x, y), a).subs(mymap)))
python fft sympy complex-numbers
python fft sympy complex-numbers
asked Nov 21 '18 at 22:45
mastercciolimasterccioli
82
82
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If you know the imaginary part is 0 then you can just take the real part of the evaluation, else use "chop=True" with caution to discard relatively small imaginary parts:
>>> q
(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 +
5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
>>> q.n(chop=True)
-0.170146237401735
>>> re(q.n())
-0.170146237401735
add a comment |
Your Answer
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: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53421480%2fsympy-simplify-eliminate-imaginary-numbers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you know the imaginary part is 0 then you can just take the real part of the evaluation, else use "chop=True" with caution to discard relatively small imaginary parts:
>>> q
(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 +
5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
>>> q.n(chop=True)
-0.170146237401735
>>> re(q.n())
-0.170146237401735
add a comment |
If you know the imaginary part is 0 then you can just take the real part of the evaluation, else use "chop=True" with caution to discard relatively small imaginary parts:
>>> q
(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 +
5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
>>> q.n(chop=True)
-0.170146237401735
>>> re(q.n())
-0.170146237401735
add a comment |
If you know the imaginary part is 0 then you can just take the real part of the evaluation, else use "chop=True" with caution to discard relatively small imaginary parts:
>>> q
(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 +
5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
>>> q.n(chop=True)
-0.170146237401735
>>> re(q.n())
-0.170146237401735
If you know the imaginary part is 0 then you can just take the real part of the evaluation, else use "chop=True" with caution to discard relatively small imaginary parts:
>>> q
(-1.53283653303955 + 6.08703605256546e-17*I)/(sqrt(5.69974497311137 +
5.55111512312578e-17*I)*sqrt(14.2393958011541 - 3.46944695195361e-18*I))
>>> q.n(chop=True)
-0.170146237401735
>>> re(q.n())
-0.170146237401735
answered Nov 22 '18 at 13:31
smichrsmichr
3,5461011
3,5461011
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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.
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%2fstackoverflow.com%2fquestions%2f53421480%2fsympy-simplify-eliminate-imaginary-numbers%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