complex-numbers

Sympy Simplify eliminate imaginary numbers

帅比萌擦擦* 提交于 2019-12-24 05:31:36
问题 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),

Are there any Haskell libraries for integrating complex functions?

帅比萌擦擦* 提交于 2019-12-24 00:58:28
问题 How to numerically integrate complex, complex-valued functions in Haskell? Are there any existing libraries for it? numeric-tools operates only on reals. I am aware that on complex plane there's only line integrals, so the interface I am interested in is something like this: i = integrate f x a b precision to calculate integral along straight line from a to b of function f on point x . i , x , a , b are all of Complex Double or better Num a => Complex a type. Please... :) 回答1: You can make

How to raise arrays with negative values to fractional power in Python?

喜欢而已 提交于 2019-12-24 00:14:56
问题 I have an array with negative values that has to be raised to fractional power in Python. I need to obtain the real part of the complex number array generated by the operation. MWE from __future__ import division import numpy as np a = -10 b = 2.5 n = 0.88 x = np.arange(5, 11, 1) y = (a / (x - b)) ** (1 / n) I am using Python v2.7.6. 回答1: The issue is that NumPy does not promote float or integer dtypes to complex dtypes for this calculation. You have a float array base and a float exponent,

In what cases does Python complex exponentiation throw an OverflowError?

风格不统一 提交于 2019-12-23 14:23:11
问题 I’m trying to figure out the pattern here: >>> 1e300 ** 2 OverflowError: (34, 'Result too large') >>> 1e300j ** 2 OverflowError: complex exponentiation >>> (1e300 + 1j) ** 2 OverflowError: complex exponentiation >>> (1e300 + 1e300j) ** 2 (nan+nanj) The behavior seems to be not only unspecified in theory, but also weird in practice! What explains this? 回答1: A look at the source for complex exponentiation shows that Python only checks for overflow at the end of the computation. Also, there's a

Multiplying complex with constant in C++

删除回忆录丶 提交于 2019-12-23 11:16:08
问题 The following code fails to compile #include <iostream> #include <cmath> #include <complex> using namespace std; int main(void) { const double b=3; complex <double> i(0, 1), comp; comp = b*i; comp = 3*i; return 0; } with error: no match for ‘operator*’ in ‘3 * i’ What is wrong here, why cannot I multiply with immediate constants? b*i works. 回答1: In the first line: comp = b*i; The compiler calls: template<class T> complex<T> operator*(const T& val, const complex<T>& rhs); Which is instanced as

Boost multiprecision fails because the implementation of complex tries to cast to double in internal functions like _Isinf or _Isnan

♀尐吖头ヾ 提交于 2019-12-23 09:58:13
问题 I need a BSD-like licensed C(++) multiprecision library with complex numbers support so I tried boost. The following code fails: #include <boost/multiprecision/cpp_dec_float.hpp> #include <complex> using namespace boost::multiprecision; std::complex<cpp_dec_float_50>(1.0, 2.0) / std::complex<cpp_dec_float_50>(1.0, 2.0) in Visual Studio 2012 with an error C2440 because the implementation of complex tries to cast to double in internal functions like _Isinf or _Isnan. Is this an error on my part

How to get Vector of Complex numbers from two vectors (real & imag)

久未见 提交于 2019-12-23 03:44:12
问题 I have two vectors of floats and i want them to become one vector of Complex numbers. I'm stuck. I don't mind using iterators, but i am sure it'd be rediscovering the wheel i'm not informed about. Is my code leading me in the right direction? typedef std::vector<float> CVFloat; CVFloat vA, vB; //fil vectors typedef std::complex<CVFloat> myComplexVector; myComplexVector* vA_Complex = new myComplexVector(vA, vB); The code above is going through the compiler correctly, but when i want to get

Python: How to write a complex number to excel using xlwt?

女生的网名这么多〃 提交于 2019-12-22 18:32:15
问题 I am trying to write a Python list to an excel file using xlwt library. import xlwt from tempfile import TemporaryFile book = xlwt.Workbook() sheet1 = book.add_sheet('sheet1') for i in range(len(newdata)): for j in range(len(newdata[i])): sheet1.write(i,j,newdata[i][j]) name = "my_file.xls" book.save(name) book.save(TemporaryFile()) It work for common variable types (e.g. int, float, string) but when I try to write a complex number to the excel file, I get the following error: Exception:

SWIG: How to pass list of complex from c++ to python

早过忘川 提交于 2019-12-22 12:09:33
问题 I have a function in c++ who returns a list of complex: #include <complex> std::list< std::complex<double> > func(...); what should i do in the '*.i' file ? Thank you every body. ================= Following are details: the function i would like to use in python in x.h: std::list<std::complex<double> > roots1(const double& d, const double& e); I have tried some ways: (1) x.i: %include <std_list.i> %include <std_complex.i> than I try in IPython: tmp = x.roots1(1.0, 2.0) len(tmp) and I got: ---

Unwrap angle to have continuous phase

给你一囗甜甜゛ 提交于 2019-12-22 07:39:12
问题 Let's say I have an array of phases similar to this: import numpy as np import matplotlib.pyplot as plt phase = np.linspace(0., 100., 1000) % np.pi plt.plot(phase) plt.show() (with many discontinuities like this) How to get an array of more "continuous" phases from it? Of course, I already tried with np.unwrap: plt.plot(np.unwrap(phase)) or plt.plot(np.unwrap(phase),discont=0.1) but it stays exactly similar: What I expected was an unwrapping like this: 回答1: If you want to keep your original