From Test-Scratch-Wiki


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.

Representing a complex number on a plane

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


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

Subtraction


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

Multiplication


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

Division


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


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

Cube


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

Inverse


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.
([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])

[[en:How to Use Complex Numbers in Scratch}}

Cookies help us deliver our services. By using our services, you agree to our use of cookies.