This tutorial shows how to calculate complex numbers in Scratch. A complex number is a number with a real part and an "imaginary" part, which is equal to the square root of -1.

Preparing

Because complex numbers have both a real and an imaginary part, six variables are needed - two for each input and two more for the answer.

(a :: variables) // The 1st real part
(b :: variables) // The 1st imaginary part
(c :: variables) // The 1st real part
(d :: variables) // The 2nd imaginary part
(e::variables) // The real part of the answer
(f::variables) // The imaginary part of the answer

Addition

(a + bi) + (c + di) = (a + c) + (b + d)i

set [e v] to ((a) + (c))
set [f v] to ((b) + (d))

Subtraction

(a + bi) - (c + di) = (a - c) + (b - d)i

set [e v] to ((a) - (c))
set [f v] to ((b) - (d))

Multiplication

(a + bi)(c + di) = ac + adi + bci - bd

set [e v] to (((a) * (c)) - ((b) * (d)))
set [f v] to (((a) * (d)) + ((b) * (c)))

Division

(a + bi) / (c + di) = (ac - adi + bci + bd) / (c2 + d2)

set [e v] to ((((a) * (c)) + ((b) * (d))) / (((c) * (c)) + ((d) * (d))))
set [f v] to ((((b) * (d)) - ((a) * (c))) / (((c) * (c)) + ((d) * (d))))


Exponents

Square

(a + bi)2 = a2 + 2abi - b2

set [e v] to (((a) * (a)) - ((b) * (b)))
set [f v] to ((2) * ((a) * (b)))

Cube

(a + bi)3 = a3 + 3a2bi - 3ab2 - b3i

set [e v] to (((a) * ((a) * (a))) - ((3) * ((a) * ((b) * (b)))))
set [f v] to (((3) * ((a) * ((a) * (b)))) - ((b) * ((b) * (b))))

Inverse

(a + bi)-1 = (a - bi) / (a2 + b2)

set [e v] to ((a) / (((a) * (a)) + ((b) * (b)))
set [f v] to ((0) - ((b) / (((a) * (a)) + ((b) * (b)))

Absolute value

The absolute value of a complex number measures its distance from 0 using the Pythagorean theorem. |a + bi| = √(a2 + b2)
([sqrt v] of (((a) * (a)) + ((b) * (b))))

Displaying values

The scripts listed in this tutorial all store the answer as two separate values. To display the answer to the user, the following script may be used:

if <(b) \< (0)> then
    set [answer v] to (join(e::variables)(join[ - ](join((0) - (f))[i])
else
    set [answer v] to (join(e::variables)(join[ + ](join(f)[i])
Cookies help us deliver our services. By using our services, you agree to our use of cookies.