This post's goal is to get some experience performing calculations using Julia. All these examples can be done in the REPL, but we'll use Jupyter here.
The first program that EVERYBODY writes in a new language is the so-called "Hello World" program. Who are WE to break with such a stupid time-honored tradition??
xxxxxxxxxx
println("Hello, world!")
Hello, world!
Now that we got that bromide out of the way, lets do something a little more interesting!
Comments¶
xxxxxxxxxx
# This is a one line comment.
# The pound sign at the start tells Julia to ignore this line.
#=
If there are more extensive comments, say that go across multiple lines,
you can surround surround those lines with #= and =#.
Comments are for our benefit, like explaining non-obvious code so it is easier for others to read our code,
or so we can read it six months from now!
Comments can also be used for debugging purposes, which we'll see later.
=#
#=
The only inconvience you can encounter with comments is in Jupyter notebooks:
the pound sign found at the start of a comment
is the same as the as the header tag in Markdown!
=#
Calculations¶
Here's how to perform some basic calculations - nothing that can't be done with a pocket calculator.
xxxxxxxxxx
# Addition
342 + 54.88 + -544
-147.12
xxxxxxxxxx
# Subtraction
54 - 1115
-1061
xxxxxxxxxx
# Multiplication
88 * 11
968
xxxxxxxxxx
# Division
5 / 23
0.21739130434782608
xxxxxxxxxx
# Exponentiation
7 ^ 3
343
xxxxxxxxxx
# All together now!
3*5 - 17 + 8/4^2
-1.5
xxxxxxxxxx
# Use parentheses to control order of operations
# If no parentheses are used, the default is exp-mult-div-add-subtract
3*5 - 17 + (8/4)^2
2.0
xxxxxxxxxx
3*(5 - 17) + (8/4)^2
-32.0
xxxxxxxxxx
# Quotient, also called div or integer division
# This is the number of times 5 goes into 26
# To get the divison sign, type \div then press TAB
26 ÷ 4
6
xxxxxxxxxx
# Same as above
div(26, 4)
6
xxxxxxxxxx
# Remainder, also called modulus or mod
# This is how much is left over from the div
26 % 4
2
xxxxxxxxxx
# Same as above
rem(26, 4)
2
Variables¶
xxxxxxxxxx
# Let's store the above results into variables
q = 26 ÷ 4
r = 26 % 4
2
xxxxxxxxxx
# Notice that only the result of the last line was displayed. Was q assigned a value? Let's find out:
q
6
xxxxxxxxxx
# So it was!
# Another way to see this is to use the println function:
println("q = ", q)
q = 6
xxxxxxxxxx
# To supress output from calculations, add a semicolon on the end like this:
q = 26 ÷ 4
r = 26 % 4;
xxxxxxxxxx
# One way to display both values is to use two println functions in succession:
println("q = ", q)
println("r = ", r)
q = 6 r = 2
xxxxxxxxxx
# Let's put quotient and remainder together - result should be what we started with, 26
4*q + r
26
xxxxxxxxxx
# In the above line, the multiplication sign (*) can be omitted,
# as long as there is no space between the 4 and the q
4q + r
26
xxxxxxxxxx
#=
Because Julia variables can have unicode characters in them, you can use Greek letters found in mathematics,
statistics, physics, etc.
For example, to make a variable named theta as in the Greek letter θ, type \theta then the TAB key,
then assign a value to it.
=#
θ = 45
45
Trigonometric Functions and Some Constants¶
Julia has the usual trig, log and exponential functions built in, along with some constants
xxxxxxxxxx
# The usual trig functions are sin(x), cos(x), tan(x), cot(x), sec(x), csc(x)
sin(0)
0.0
xxxxxxxxxx
tan(45)
1.6197751905438615
xxxxxxxxxx
# Starting a line with #
xxxxxxxxxx
# Notice that the trig functions assume you're giving them an angle measured in radians. Instead of converting
# to degrees, there are special trig functions for working with degrees:
# sind(x), cosd(x), tand(x), cotd(x), secd(x), cscd(x)
tand(45)
1.0
xxxxxxxxxx
# There is a built-in constant for pi, which can be entered as \pi then press TAB
π
π = 3.1415926535897...
xxxxxxxxxx
cos(π)
-1.0
xxxxxxxxxx
cos(3π/4)
-0.7071067811865475
xxxxxxxxxx
# There are also inverse trig functions as well as hyperbolic functions
Log and Exponential Functions¶
xxxxxxxxxx
# The log(x) is the *natural* log
log(100)
4.605170185988092
xxxxxxxxxx
# To get the base-10 log, do this:
log10(100)
2.0
xxxxxxxxxx
# For base-2 logs, (a/k/a binary logs), use log2(x)
log2(2^43)
43.0
xxxxxxxxxx
# There is a built-in constant for Euler's constant e, which can be entered as \euler then press TAB
ℯ
ℯ = 2.7182818284590...
xxxxxxxxxx
# Note that if you just type "e", you'll get an error!
e
UndefVarError: e not defined Stacktrace: [1] top-level scope @ :0 [2] eval @ ./boot.jl:360 [inlined] [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String) @ Base ./loading.jl:1094
xxxxxxxxxx
# If you do get an error, just fix it and move on.
# Remember: in Julia no one can hear you scream.
xxxxxxxxxx
log(ℯ^23)
23.0
xxxxxxxxxx
# The exponential function ℯ^x
exp(6)
403.4287934927351
xxxxxxxxxx
# That should be the same as ℯ^6
ℯ^6
403.4287934927351
xxxxxxxxxx
Roots¶
xxxxxxxxxx
sqrt(2)
1.4142135623730951
xxxxxxxxxx
cbrt(2)
1.2599210498948732
Rounding Functions¶
xxxxxxxxxx
# Round to the nearest integer
round(2.98)
3.0
xxxxxxxxxx
# Round down
floor(2.98)
2.0
xxxxxxxxxx
# Round up - ceiling
ceil(5.0004)
6.0
GCD and LCM¶
xxxxxxxxxx
# Greatest Common Divisor
gcd(49, 14, 7, 35)
7
xxxxxxxxxx
# Least Common Multiple
lcm(7, 14, 49)
98
Conclusion¶
So you get the idea:
- Julia has a large number of built-in functions
- There are some built-in constants like \pi and \euler
- To store a calculation into a variable, do this: var = value
- To output a variable called x, just type it or use println(x)
- Single-line comments are started with a #
- Multi-line comments are wrapped in #= and =#
For a list of the built-in functions, see this page: https://docs.julialang.org/en/v1/manual/mathematical-operations/
Another take-away from this introduction is that Jupyter allows you to experiment with your calculations - think of it as a playground. The advantage of Jupyter over the REPL is that you can easily save your work, share it with others, and etc.
No comments:
Post a Comment