Sympy Simplify eliminate imaginary numbers
up vote
1
down vote
favorite
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 |
up vote
1
down vote
favorite
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 |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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 at 22:45
masterccioli
82
82
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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 |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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 |
up vote
0
down vote
accepted
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 |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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 at 13:31
smichr
3,368710
3,368710
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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