Julia, the giant calculator

Simple arithmetic

Addition, subtraction, multiplication, division and powers by arithmetic operators


In [9]:
#Addition using +
2 + 2


Out[9]:
4

In [10]:
#Subtraction using -
3 - 4


Out[10]:
-1

In [11]:
#Multiplication using *
3 * 4


Out[11]:
12

In [12]:
#Division using /
12 / 4


Out[12]:
3.0

In [13]:
#Powers using ^
2^4


Out[13]:
16

Simple arithmetic using in-built functions, as well as fractions


In [15]:
#typeof() (we will learn more about types in an upcoming lesson)
typeof(3.0)


Out[15]:
Float64

In [16]:
#typeof(+)
typeof(+)


Out[16]:
Function

In [18]:
#Addition using +()
+(2, 2, 4, 6, 3, 4, 7)


Out[18]:
28

In [19]:
#Subtraction using -()
-(3, 4)


Out[19]:
-1

In [20]:
#Multiplication using *()
*(3, 4)


Out[20]:
12

In [21]:
#Division using /()
/(12, 4)


Out[21]:
3.0

In [22]:
#Powers using ^()
^(2, 4)


Out[22]:
16

In [23]:
#Fractions using //
 3 // 4


Out[23]:
3//4

In [24]:
#The numerator num()
num(3 // 4)


Out[24]:
3

In [25]:
#The denominator den()
den(3 // 4)


Out[25]:
4

In [26]:
#Simplification using fractions, ie.e 6 // 9
6 // 9


Out[26]:
2//3
  • The remainder is what is left after division, as in $ \frac{5}{3} = 1 \frac{2}{3} $
  • The $ 1 $ is the floor and the $ 2 $ is the remainder

In [27]:
#Calculating the remainder of division using rem()
rem(5, 3)


Out[27]:
2

In [28]:
#Calculating both the floor and the remainder using divrem()
divrem(5, 3)


Out[28]:
(1,2)

Association

  • The order of operations are
    • .
    • ::
    • ^ (and its element-by-elemnt equivalent .^
    • // and .//
      • and / and %
    • Bitshifts
      • and -
    • Syntax such as : .. |>
    • Comparisons
    • Control flow
    • Assignments

In [29]:
#Forcing order through the use of parentheses
2 - 4 * 3


Out[29]:
-10

In [30]:
#Now with parentheses
(2 - 4) * 3


Out[30]:
-6

Rounding numbers


In [31]:
#Rounding to the nearest integer using the round() function
#Example using 3.4
round(3.4)


Out[31]:
3.0

In [32]:
#To the nearest integer using the round() function
#Example using 3.5
round(3.5)


Out[32]:
4.0

In [33]:
#Towards positive infinity using the ceil() function
#Example using 3.001
ceil(3.001)


Out[33]:
4.0

In [34]:
#Towards positive infinity using the ceil() function
#Example using -3.999
ceil(-3.999)


Out[34]:
-3.0

In [35]:
#Towards negative infinity using floor()
#Example using 3.999
floor(3.999)


Out[35]:
3.0

In [36]:
#Towards negative infinity using the floor() function
#Example using -3.001
floor(-3.001)


Out[36]:
-4.0

In [37]:
#Towards zero using the trunc() function
#Example using 3.999
trunc(3.999)


Out[37]:
3.0

In [38]:
#Towards zero using the trunc() function
#Example using -3.999
trunc(-3.999)


Out[38]:
-3.0

Greatest common divisor and least common multiple


In [39]:
#Finding the greatest common divisor using the gcd() function
#What is the greatest numbers that will divide into 4, 8, and 20?
gcd(4, 8, 20)


Out[39]:
4

In [40]:
#Finding the least common multiple using the lcm() function
#What is the least common multiple of 2, 3, 5, 10 (takes successive multiple of all these values and find
#the least (lowest) common value)?
lcm(2, 3, 5, 10)


Out[40]:
30

Absolute values

$ \left| -a \right| = a $


In [41]:
#Calculating the absolute value using the abs() function
abs(-10)


Out[41]:
10

$ {\left| -a \right|}^{2} $


In [42]:
#Calculating the absolute valued squared using the abs2() function
abs2(-10)


Out[42]:
100

Trigonometric functions

$ \sin \cos \ \tan \\ \csc \sec \cot \\ \sinh \cosh \tanh $ and many more


In [43]:
#Calculating the sine of pi using sin(pi)
sin(pi)


Out[43]:
1.2246467991473532e-16

In [44]:
#Usingthe d-ending for degrees, i.e. sind(180)
sind(180)


Out[44]:
0.0

In [45]:
#Calculating the hypotenuse of a right-angled triangle using the hypot() function
hypot(3, 4)


Out[45]:
5.0

The sign of a value (as in positive or negative or none for zero)

  • Returns +1 for positive values
  • Returns -1 for negative values
  • Returns 0 for 0

In [47]:
#Using sign() for the sign of the sine of pi
sign(sind(180))


Out[47]:
0.0

Numeric comparisons

  • Using Boolean logic to find out if values are equal on unequal, i.e. greater than or less than each other

In [48]:
#Comparison using ==
3 == 3


Out[48]:
true

In [49]:
#Is greater than using >
3 > 3


Out[49]:
false

In [50]:
#Is greater than or equal to using >=
3 >= 3


Out[50]:
true

In [51]:
#Is less than using <
3 < 3


Out[51]:
false

In [52]:
#Is less than or equal to using <=
3 <= 3


Out[52]:
true

In [53]:
#Is not equal to using !=
3 != 3


Out[53]:
false
  • The special cases of NaN, Inf and -Inf

In [54]:
#What is the type of NaN using typeof(NaN)
typeof(NaN)


Out[54]:
Float64

In [55]:
#Is NaN equal to NaN?
NaN == NaN


Out[55]:
false

In [56]:
#Is NaN greater than NaN
NaN > NaN


Out[56]:
false

In [57]:
#What is the type of Inf?
typeof(Inf)


Out[57]:
Float64

In [59]:
#Is Inf + 1 greater than Inf?
(Inf + 1) < Inf


Out[59]:
false
  • Logical tests using functions

In [60]:
#Comparing values using the isequal(x, y) function
#Just FYI, isequal(NaN, NaN) is true
isequal(3, 3)


Out[60]:
true

In [61]:
#Checking to see of a value is finite using the isfinite() function
isfinite(3)


Out[61]:
true

In [62]:
#Checking to see if a number is infinite using the isinf() function
isinf(3)


Out[62]:
false

In [63]:
#Checking to see if a value is NaN using the isnan() function
isnan(NaN)


Out[63]:
true

In [64]:
#The peculiarity of -0.0 == 0.0 vs isequal(-0.0, 0.0)
-0.0 == 0


Out[64]:
true

In [65]:
#
isequal(-0.0, 0)


Out[65]:
false

In [66]:
#We could also use ==() instead of isequal(), etc...
==(3, 3)


Out[66]:
true

Special powers (roots) and logarithms


In [67]:
#Calculating the square roots using the sqrt() function
sqrt(9)


Out[67]:
3.0

In [68]:
#Calculating the cube roots using the cbrt() function
cbrt(27)


Out[68]:
3.0

In [69]:
#Calculating the natural exponential using the exp() function
exp(1)


Out[69]:
2.718281828459045

In [70]:
#For more accuracy when x is near zero in the case of exp(x-1) use the expm1(x) function
expm1(0.0001)


Out[70]:
0.00010000500016667084

In [71]:
#Calculating the natural logarithm using the log() function
log(100)


Out[71]:
4.605170185988092

In [72]:
#Specifying a base for the logarithm using the log(base, value) function (there are also the log2() and log10() functions)
log(10, 100)


Out[72]:
2.0

In [73]:
#For more accuracy when x is near zero in the case of log(1+x) use the log1p() function
log1p(0.0001)


Out[73]:
9.999500033330834e-5

Special functions

  • The error function at x
    • erf(x)
  • The inverse function to the error function
    • erfinv(x)
  • The gamma function at x
    • gamma(x)
  • The beta function at x
    • beta(x)
  • Many others...

The complex numbers


In [1]:
#The notation for a complex number using im
2 + 2im


Out[1]:
2 + 2im

In [2]:
#Constructing a complex number using the complex() function
complex(2, 2)


Out[2]:
2 + 2im

In [3]:
#The real part of a complex number using the real() function
real(2 + 2im)


Out[3]:
2

In [5]:
#The imaginary part of a complex number using the imag() function
imag(2 + 4im)


Out[5]:
4

In [7]:
#The complex conjugate of a complex number using the conj() function
conj(2 - 2im)


Out[7]:
2 + 2im

In [9]:
#The absolute value of a complex number using the abs() function
abs(3 + 4im)


Out[9]:
5.0

In [10]:
#The square of the absolute value using the abs2() function
abs2(3 + 4im)


Out[10]:
25

In [11]:
#The argument (in radians) of a complex number using the angle() function
angle(3 + 4im)


Out[11]:
0.9272952180016122

In [12]:
#Calculating the square root of -1 using sqrt(1im^2), that is, calculating the imaginary number
sqrt(1im^2)


Out[12]:
0.0 + 1.0im

Simple plotting using Gadfly


In [1]:
using Gadfly

In [2]:
f(x) = sin(x)


Out[2]:
f (generic function with 1 method)

In [3]:
g(x) = cos(x)


Out[3]:
g (generic function with 1 method)

In [4]:
plot([f, g], -2pi, 2pi)


Out[4]:
x -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 -40 -20 0 20 40 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 f1 f2 Color -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 f(x)

In [88]:
plot([sin, cos], -2pi, 2pi)


Out[88]:
x -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 -30 -29 -28 -27 -26 -25 -24 -23 -22 -21 -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 -40 -20 0 20 40 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 f1 f2 Color -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 f(x)

In [83]:
h(x) = (x - 1)^3 + 1


Out[83]:
h (generic function with 1 method)

In [84]:
plot(h, -5, 5)


Out[84]:
x -20 -15 -10 -5 0 5 10 15 20 -15.0 -14.5 -14.0 -13.5 -13.0 -12.5 -12.0 -11.5 -11.0 -10.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 -20 -10 0 10 20 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -800 -700 -600 -500 -400 -300 -200 -100 0 100 200 300 400 500 600 -700 -680 -660 -640 -620 -600 -580 -560 -540 -520 -500 -480 -460 -440 -420 -400 -380 -360 -340 -320 -300 -280 -260 -240 -220 -200 -180 -160 -140 -120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 320 340 360 380 400 420 440 460 480 500 -1000 -500 0 500 -700 -650 -600 -550 -500 -450 -400 -350 -300 -250 -200 -150 -100 -50 0 50 100 150 200 250 300 350 400 450 500 f(x)

In [85]:
plot(x -> 1 / x, -4, 4)


Out[85]:
x -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 -12.0 -11.5 -11.0 -10.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 -20 -10 0 10 20 -12.0 -11.5 -11.0 -10.5 -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 -350 -300 -250 -200 -150 -100 -50 0 50 100 150 200 250 300 350 -300 -290 -280 -270 -260 -250 -240 -230 -220 -210 -200 -190 -180 -170 -160 -150 -140 -130 -120 -110 -100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 260 270 280 290 300 -400 -200 0 200 400 -300 -280 -260 -240 -220 -200 -180 -160 -140 -120 -100 -80 -60 -40 -20 0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300 f(x)

In [86]:
plot(x -> 1 / x, 0, 5)


Out[86]:
x -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0 -5 0 5 10 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100 110 -50 -48 -46 -44 -42 -40 -38 -36 -34 -32 -30 -28 -26 -24 -22 -20 -18 -16 -14 -12 -10 -8 -6 -4 -2 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 -50 0 50 100 -50 -45 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 f(x)

In [87]:
plot(x -> erf(x), -3, 3)


Out[87]:
x -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 -9.0 -8.8 -8.6 -8.4 -8.2 -8.0 -7.8 -7.6 -7.4 -7.2 -7.0 -6.8 -6.6 -6.4 -6.2 -6.0 -5.8 -5.6 -5.4 -5.2 -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 -10 -5 0 5 10 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0 -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5 -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2 -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 -4 -2 0 2 4 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 f(x)

In [ ]: