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

Representing a complex number on a plane

Preparation

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.

(real input 1:: variables) // The 1st real part
(imaginary input 1:: variables) // The 1st imaginary part
(real input 2:: variables) // The 2nd real part
(imaginary input 2:: variables) // The 2nd imaginary part
(real answer::variables) // The real part of the answer
(imaginary answer::variables) // The imaginary part of the answer

Addition

Two complex numbers can be added together by simply adding the real and complex parts separately:


set [real answer v] to ((real input 1) + (real input 2))
set [imaginary answer v] to ((imaginary input 1) + (imaginary input 2))

Subtraction

Two complex numbers can be subtracted by subtracting the real and complex parts separately:


set [real answer v] to ((real input 1) - (real input 2))
set [imaginary answer v] to ((imaginary input 1) - (imaginary input 2))

Multiplication

Two complex numbers can be multiplied using the distributive property:


The real and imaginary parts can then be combined together:


set [real answer v] to (((real input 1) * (real input 2)) - ((imaginary input 1) * (imaginary input 2)))
set [imaginary answer v] to (((real input 1) * (imaginary input 2)) + ((imaginary input 1) * (real input 2)))

Division

Two complex numbers can be divided by converting it into one fraction, and then multiplying both the numerator and denominator by the denominator's complex conjugate:


The fraction can then be separated into its real and imaginary part:


set [real answer v] to ((((real input 1) * (real input 2)) + ((imaginary input 1) * (imaginary input 2))) / (((real input 2) * (real input 2)) + ((imaginary input 2) * (imaginary input 2))))
set [imaginary answer v] to ((((real input 2) * (imaginary input 1)) - ((real input 1) * (imaginary input 2))) / (((real input 2) * (real input 2)) + ((imaginary input 2) * (imaginary input 2))))

Exponents

As the base

Square

A complex number can be squared using the distributive property:


This result can then be simplified and separated into its real and imaginary part:


set [real answer v] to (((real input 1) * (real input 1)) - ((imaginary input 1) * (imaginary input 1)))
set [imaginary answer v] to ((2) * ((real input 1) * (imaginary input 1)))

Cube

A complex number can be cubed using the distributive property:


This result can then be simplified and separated into its real and imaginary parts:


set [real answer v] to (((real input 1) * ((real input 1) * (real input 1))) - ((3) * ((real input 1) * ((imaginary input 1) * (imaginary input 1)))))
set [imaginary answer v] to (((3) * ((real input 1) * ((real input 1) * (imaginary input 1)))) - ((imaginary input 1) * ((imaginary input 1) * (imaginary input 1))))

Square Root


set [real answer v] to ([sqrt v] of ((([sqrt v] of (((real input 1) * (real input 1)) + ((imaginary input 1) * (imaginary input 1)))) + (real input 1)) / (2))
set [imaginary answer v] to (((imaginary input 1)/([abs v] of (imaginary input 1))) * ([sqrt v] of ((([sqrt v] of (((real input 1) * (real input 1)) + ((imaginary input 1) * (imaginary input 1)))) - (real input 1)) / (2))))

Reciprocal

The reciprocal of a complex number can be found by converting it into a fraction, then multiplying the numerator and denominator by the denominator's conjugate:


The result can then be simplified and split into its real and imaginary part:


set [real answer v] to ((real input 1) / (((real input 1) * (real input 1)) + ((imaginary input 1) * (imaginary input 1)))
set [imaginary answer v] to ((0) - ((imaginary input 1) / (((real input 1) * (real input 1)) + ((imaginary input 1) * (imaginary input 1)))

As the exponent

e^

e to the power of a complex number can be found by first splitting the addition in the exponent into a product of two exponents, and then expanding the second exponent using Euler's formula:


Because Scratch expects the trigonometric functions' inputs to be in degrees rather than radians, must be multiplied to the inputs of trigonometric functions.


set [real answer v] to (([e ^ v] of (real input 1)) * (([cos v] of (imaginary input 1)) * (57.29577951308232)))
set [imaginary answer v] to (([e ^ v] of (real input 1)) * (([sin v] of (imaginary input 1)) * (57.29577951308232)))

10^

10 to the power of a complex number can be found by first splitting the addition in the exponent into a product of two exponents, and then converting the imaginary power into exponential form:

The exponent in exponential form can then be expanded using Euler's formula:


Because Scratch expects the trigonometric functions' inputs to be in degrees rather than radians, must be multiplied to the inputs of trigonometric functions.

set [real answer v] to (([10 ^ v] of (real input 1)) * ([cos v] of ((imaginary input 1) * (131.928407798297))
set [imaginary answer v] to (([10 ^ v] of (real input 1)) * ([sin v] of ((imaginary input 1) * (131.928407798297))

Logarithms

Natural Log


set [real answer v] to (([ln v] of (((real input 1) * (real input 1)) + ((imaginary input 1) * (imaginary input 1)))) / (2))
set [imaginary answer v] to (([atan v] of ((imaginary input 1) / (real input 1))) * (0.017453292519943295))

Base-10 Log


set [real answer v] to (([ln v] of (((real input 1) * (real input 1)) + ((imaginary input 1) * (imaginary input 1)))) / (4.605170185988092))
set [imaginary answer v] to (([atan v] of ((imaginary input 1) / (real input 1))) * (0.007579868632454674))

Trigonometric Functions

Note Note: The trigonometric functions listed below output their answer in radians.

Sin

The sine of a complex number can be found by first using the sine angle addition formula:

Hyperbolic functions can then be substituted for sine and cosine functions with imaginary inputs, which can then be replaced with their definition:


Because Scratch expects the trigonometric functions' inputs to be in degrees rather than radians, must be multiplied to the inputs of trigonometric functions.

set [real answer v] to (([sin v] of ((real input 1) * (57.29577951308232))) * ((([e ^ v] of (imaginary input 1)) + ([e ^ v] of ((0) - (imaginary input 1)))) / (2)))
set [imaginary answer v] to (([cos v] of ((real input 1) * (57.29577951308232))) * ((([e ^ v] of (imaginary input 1)) - ([e ^ v] of ((0) - (imaginary input 1)))) / (2)))

Cos

Much like finding the sine of a complex number, the cosine of a complex number can be found by using the cosine angle addition formula, substituting a hyperbolic function and its definition, and then multiplying trigonometric functions' inputs by .


set [real answer v] to (([cos v] of ((real input 1) * (57.29577951308232))) * ((([e ^ v] of (imaginary input 1)) + ([e ^ v] of ((0) - (imaginary input 1)))) / (2)))
set [imaginary answer v] to ((0) - (([sin v] of ((real input 1) * (57.29577951308232))) * ((([e ^ v] of (imaginary input 1)) - ([e ^ v] of ((0) - (imaginary input 1)))) / (2))))

Tan


set [real answer v] to (([sin v] of ((114.59155902616465) * (real input 1))) / (([cos v] of ((114.59155902616465) * (real input 1))) + ((([e ^ v] of ((2) * (imaginary input 1))) + ([e ^ v] of ((-2) * (imaginary input 1)))) / (2))))
set [imaginary answer v] to (((([e ^ v] of ((2) * (imaginary input 1))) - ([e ^ v] of ((-2) * (imaginary input 1)))) / (2)) / (([cos v] of ((114.59155902616465) * (real input 1))) + ((([e ^ v] of ((2) * (imaginary input 1))) + ([e ^ v] of ((-2) * (imaginary input 1)))) / (2))))

Rounding

Round

A complex number can be rounded by simply rounding both the real and imaginary components.

set [real answer v] to (round(real input 1))
set [imaginary answer v] to (round(imaginary input 1))

Magnitude

The magnitude of a complex number measures its distance from 0 using the Pythagorean theorem.
([sqrt v] of (((real input 1) * (real input 1)) + ((imaginary input 1) * (imaginary input 1))))

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 <(imaginary answer) \< (0)> then
    set [answer v] to (join(real answer::variables)(join[ - ](join((0) - (imaginary answer))[i])
else
    set [answer v] to (join(real answer::variables)(join[ + ](join(imaginary answer)[i])
Cookies help us deliver our services. By using our services, you agree to our use of cookies.