complex-numbers

How to Print Out the Square Root of A Negative Number with i Displayed in C

别说谁变了你拦得住时间么 提交于 2019-12-14 03:34:45
问题 I am trying to figure out how to display the square root of a number if it happens to be negative (as it is entered by the user), and if so, display it correctly with the "i" displayed as well. When I do the normal sqrt function, the result is always something like -1.#IND. When I tried using the double complex variables, the positive numbers nor the negative numbers would come out clean. Below is my code; the comments are what my goal is. The 4 num variables are entered by the user and can

Python Division Of Complex Numbers Without Using Built In Types and Operators

前提是你 提交于 2019-12-14 01:17:53
问题 I have to implement a class called ComplexNumbers which is representing a complex number and I'm not allowed to use the built in types for that. I already have overwritten the operators ( __add__ , __sub__ , __mul__ , __abs__ , __str_ which allows to perform basic operations. But now I'm stuck with overwriting the __div__ operator. Allowed to use: I'm using float to represent the imaginary part of the number and float to represent the rel part. What I have already tried: I looked up how to

Complex numbers: convert SymPy to numeric (I to 1j)

吃可爱长大的小学妹 提交于 2019-12-13 17:22:03
问题 using symbolic calculation in Python I have import sympy from cmath import * from mpmath import arg, cplot z = sympy.symbols('z') fhandle='z**2' g = lambda w: sympy.sympify(fhandle).evalf(subs={z: w}) g(1+2j) # Returns: -3.0 + 4.0*I # hence the next command fails, because I is expected to be 1j cplot(g, [-3,3], [-3,3]) Crawling the web I only found this which will fix the matter for the print command, but will not work with cplot. Any suggestions? 回答1: One option is to wrap the result by

Calculating nth Roots of Unity in Python

ε祈祈猫儿з 提交于 2019-12-13 15:35:39
问题 So, I'm trying to write an algorithm croot(k, n), that returns the kth root of unity with n == n. I'm getting mostly the right answer, however it's giving me really weird representations that seem wrong for certain numbers. Here is an example. import cmath def croot(k, n): if n<=0: return None return cmath.exp((2 * cmath.pi * 1j * k) / n) for k in range(8): print croot(k, 8) Output is: (1+0j) (0.70710...+0.70710...j) (6.12323399574e-17+1j) Whoa whoa whoa. So the root when k = 2 and n = 8 is

_Complex long int

不羁的心 提交于 2019-12-13 10:33:53
问题 tl;dr; Q what does int mean in _Complex long int ? Why is it legal? Longer version I was porting some 32-bit code to be 64-bit safe. In one place I noted it uses: static __complex__ long int i32X[256]; A dumb search and replace changing "long int" for int32_t gave me a compilation error. __complex__ is an older GNUism which has been replaced by the standard _Complex. Here is a short code snippet to reproduce the issue: #include <complex.h> #include <inttypes.h> typedef _Complex long int

Sorting Complex Numbers c++

拥有回忆 提交于 2019-12-13 09:55:29
问题 I am attempting to take a complex number entered by a user (3-8i or -1+5i) and then sort it in ascending order in the same format as above by real number then imaginary if the same. I start by asking the user to enter a complex number or ctr-d to terminate the program and then sort. I then want to convert the string to a float. Once a float I want to quick sort it. Is this a proper way of doing this? I know I have a lot of errors. void QuickSort(vector <float> &vec) { quick_sort(vec, 0, vec

How to avoid getting imaginary/complex number python

我的未来我决定 提交于 2019-12-13 09:13:33
问题 I am using a python code, where one of the equations got sqrt root of negative value. I use cmath.sqrt to solve it. All answers I got from that equation are shown in imaginary/complex number (e.g. x.xxxxx j ). I don't want to get that imaginary/complex number as the code that I use subsequently cannot read those imaginary/ complex number. As such, how can I avoid not to get imaginary numbers? OR in other way, how can I convert those imaginary number into real ones? or how can I remove those

FFTW producing real instead of complex output

元气小坏坏 提交于 2019-12-13 04:44:11
问题 I'm using the following code to perform the COMPLEX IFFT of an array of complex numbers (I must get complex results): #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <math.h> #include <complex.h> #include <fftw3.h> int main(void) { fftw_complex *in; fftw_complex *out; double re,im; int size; int i=0; FILE *file; fftw_plan ifft; printf("Insert size"); if (scanf("%d", &size) != 1 || size < 1) return 1; in = fftw_malloc(sizeof(*in)*size); out = fftw_malloc(sizeof(*out)*size);

Is there a reason why NOT to force 8-byte alignment for complex float type?

[亡魂溺海] 提交于 2019-12-13 03:38:23
问题 This is a follow-up for this question. We have an implementation of GCC for our embedded architecture. As such we have control over some aspects of the compiler and optimizer. Such aspect may be potentially forcing the 8-byte aligned allocation of complex float objects. Generally speaking, on our architecture we can optimize access to these objects if they are properly aligned, by requiring a single double-load instruction instead of two regular loads. Just before a round of enhancements and

Convert complex<int16_t> to complex<double>

走远了吗. 提交于 2019-12-13 01:35:19
问题 Is there anyway in C++11 to do this: std::complex<int16_t> integer(42,42); std::complex<double> doub(25.5,25.5); std::complex<double> answer = integer*doub; The error is error: no match for ‘operator*’ (operand types are ‘std::complex<short int>’ and ‘std::complex<double>’) std::complex<double> answer = integer*doub; I have tried static_cast like; std::complex<double> answer = static_cast<std::complex<double>>(integer)*doub; 回答1: There's no predefined convertion from complex<double> to