Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complex number arithmetic #86

Open
mklilley opened this issue Oct 25, 2020 · 0 comments
Open

Complex number arithmetic #86

mklilley opened this issue Oct 25, 2020 · 0 comments

Comments

@mklilley
Copy link

Hi.

Thanks so much for creating this library @nicolaspanel. I'm currently using it to make an ObservableHQ notebook to solve the Schrödinger equation using Fourier transforms.

As part of the solution I need to multiply arrays of complex numbers that come out of using the FFT as described in your documentation. I could not see a method to perform the required multiplication so I created a function myself which you can see below.

function cmultiply(z1,z2) {
   
 // z3 = z1*z2 = (x1+iy1)*(x2+iy2) = (x1*x2 - y1*y2) + i(y1*x2 + x1*y2)
  
  let x1 = z1.slice(null,[0,1]) // real part of z1
  let y1 = z1.slice(null,1)     // imag part of z1
  
  let x2 = z2.slice(null,[0,1]) // real part of z2
  let y2 = z2.slice(null,1)     // imag part of z2
  
 let x3 = nj.subtract(nj.multiply(x1,x2), nj.multiply(y1,y2)) // real part (x1*x2 - y1*y2)
 let y3 = nj.add(nj.multiply(y1,x2), nj.multiply(x1,y2))      // imag part (y1*x2 + x1*y2)
  
 return  nj.concatenate([x3,y3])
  
}

I'm sure this isn't the most efficient way to do this. I was wondering whether you have any advice on how it could be done better?

Thanks for your time and thanks again for making numjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant