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:

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle 10^{a+bi}=10^a10^{bi}=10^a(e^{\ln10})^{bi}=10^ae^{i(b\ln10)}}

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

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle 10^ae^{i(b\ln10)}=10^a{\cos(b\ln10)}+10^ai{\sin(b\ln10)}}

Because Scratch expects the trigonometric functions' inputs to be in degrees rather than radians, Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \frac{180}{\pi}} must be multiplied to the inputs of trigonometric functions.

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle 10^{a+bi}=10^a{\cos\Bigl(\Bigl(\frac{180b\ln10}{\pi}\Bigr)^\circ\Bigr)}+10^ai{\sin\Bigl(\Bigl(\frac{180b\ln10}{\pi}\Bigr)^\circ\Bigr)}}

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

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \ln(a+bi)=\frac{\ln(a^2+b^2)}{2}+i \arctan\left(\frac{b}{a} \right)}

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

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \log(a+bi)=\log(a+bi)=\frac{\ln(a^2+b^2)}{2\ln(10)}+\frac{\arctan\left(\frac{b}{a} \right)}{\ln(10)}i}

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:

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \sin(a+bi)=\sin(a)\cos(bi)+\cos(a)\sin(bi)}

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

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \sin(a)\cos(bi)+\cos(a)\sin(bi)=\sin(a)\cosh(b)+i\cos(a)\sinh(b)=\sin(a)\Bigl(\frac{e^b+e^{-b}}{2}\Bigr)+i\cos(a)\Bigl(\frac{e^b-e^{-b}}{2}\Bigr)}

Because Scratch expects the trigonometric functions' inputs to be in degrees rather than radians, Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \frac{180}{\pi}} must be multiplied to the inputs of trigonometric functions.

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \sin(a+bi)=\sin\Bigl(\Bigl(\frac{180a}{\pi}\Bigr)^\circ\Bigr)\Bigl(\frac{e^b+e^{-b}}{2}\Bigr)+i\cos\Bigl(\Bigl(\frac{180a}{\pi}\Bigr)^\circ\Bigr)\Bigl(\frac{e^b-e^{-b}}{2}\Bigr)}

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 Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \frac{180}{\pi}} .

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \cos(a+bi)=\cos\Bigl(\Bigl(\frac{180a}{\pi}\Bigr)^\circ\Bigr)\Bigl(\frac{e^b+e^{-b}}{2}\Bigr)-i\sin\Bigl(\Bigl(\frac{180a}{\pi}\Bigr)^\circ\Bigr)\Bigl(\frac{e^b-e^{-b}}{2}\Bigr)}

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

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \tan(a+bi)=\frac{\sin(2a)}{\cos(2a)+\cosh(2b)}+i\frac{\sinh(2b)}{\cos(2a)+\cosh(2b)}}

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. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \mathrm{round}(a+bi)=\mathrm{round}(a)+i\mathrm{round}(b)}

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. Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle | a + bi | = \sqrt{a^2 + b^2} }
([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.