jiggzson/nerdamer

Working with complex numbers

Closed this issue · 4 comments

Another support request. I cannot find examples about how to start working with complex numbers. How do I define a complex numbers? I could declare nerdamer(1) for creating a symbol for further manipulation. How do I do the same for (1+i) for e.g.? Some pointers would be great.

@szoshi,

In nerdamer i is reserved for complex numbers by default. You do have the ability to change that with the IMAGINARY setting. You can then use the isImaginary method, which is currently undocumented, to check if the returned value is complex. This will only test the end value, not the entire expression.

console.log(nerdamer('i').isImaginary()); // true
console.log(nerdamer('1+i').isImaginary()); // true
console.log(nerdamer('a+i').isImaginary()); // true
console.log(nerdamer('5').isImaginary()); // false
console.log(nerdamer('x').isImaginary()); // false

// Use j instead
nerdamer.set('IMAGINARY', 'j');
console.log(nerdamer('i').isImaginary()); // false
console.log(nerdamer('120+j').isImaginary()); // true

You can also use the realpart and imagpart to get parts of complex number. It's under the imaginary section of the documentation.

// get the parts
var n = '5+7*j';
console.log(nerdamer.realpart(n).toString()); // 5
console.log(nerdamer.imagpart(n).toString()); // 7

Thanks. Is there a way to multiply two complex numbers?I cannot figure out the syntax to multiply (1+i) and (2+i). It would be a big help

@szoshi,

No special syntax. Just multiply. I believe you're asking how to get the answer in expanded form. You can just call the expand method. Either of the two ways below will work. Note that there other ways to achieve this but I'm just covering these two.

var a = nerdamer('1+i');
var b = nerdamer('2+i');
console.log(a.multiply(b).expand().text()); // 1+3*i
console.log(nerdamer('(i+1)*(i+2)').expand().text()); // 1+3*i

Ok, that works as expected.