Read about this material here: Julia as a calculator.
For the impatient, this project covers the use of Julia
to replace
what a calculator can do.
This workspace is a Jupyter notebook. For a bit of orientation:
In [...]
and Out[...]
labels.Markdown
in the
toolbar, this is for formatted text. You will type commands into
cells designated as “Code.”The project notebooks, like this one, offer a brief introduction into a topic and then allow you to continue within the notebook. There is also a blank notebook to use without introductory text.
It is suggested that you use the “Run” menu to “Run all cells” when these notebook are loaded.
For the most part there is no surprise, once you learn the notations:
+
, -
, *
, /
, and ^
. (Though you may find that copying and
pasting minus signs will often cause an error, as only something that
looks like a minus sign is pasted in.)
Using IJulia
, one types the following into a cell and then presses the
run button (or shift-enter):
2 + 2
4
The answer follows below the cell.
Here is how one does a slightly more complicated computation:
(2 + 3)^4/(5 + 6)
56.81818181818182
As with your calculator, it is very important to use parentheses, as appropriate, to circumvent the usual order of operations.
One aspect that can be surprising is multiplication by a literal number
does not require a *
to be typed (e.g. 2x
or 2*x
). However, when
no *
is given, the order of operation is different than may be
expected.
On a calculator, there are buttons used to compute various functions. In
Julia
, there are many pre-defined functions that serve a similar
role (In the next project you will see how to define your own).
Functions in Julia
have names and are called using parentheses to
enclose their argument(s), as with:
sin(pi/4), cos(pi/3)
(0.7071067811865475, 0.5000000000000001)
(With IJulia
, when a cell is executed only the last command computed
is displayed, the above shows that using a comma to separate commands on
the same line can be used to get two or more commands to be displayed.)
Most basic functions in Julia
have easy to guess names, though you
will need to learn some differences, such as log
is for $\ln$ and
asin
for $\sin^{-1}$. Some function names encountered in this class
include: sqrt
, cbrt
, sin
, cos
, tan
, asin
, acos
,
atan
,exp
, and log
.
The trigonometric functions use radians. For degree measure the conversion factor $\pi/180$ must be judiciously employed.
The function $f(x) = e^x$ is implemented with function exp(x)
and
not using e^x
. In base Julia e
is not defined.
Rather than have numbered memory registers like a calculator, it is easy to assign a name to a value. For example,
x = 42
42
Names can be reassigned and repurposed. However, a function name can not be repurposed as a variable name and a variable name can not be repurposed as a function name.
You will get an ERROR
message if this is tried. (A suggestion is to
use longer descriptive names or follow math conventions: use x
, y
,
z
, … for variables; f
and g
for functions; and a
,b
, … for
parameters, though even then values like h
may play a dual role)
For assigning more than one value at once, commas can be used as with:
a, b, c = 1, 2, 3
(1, 2, 3)
The equals sign, =
, in computer languages is not the same, as it
is in mathematics. In math it typically sets up an equation. On the
computer it is reserved for assignment. As such only a limited
number of expressions can occur on the left-hand side — things being
assigned.
Unlike a calculator, but just like math, Julia
has different types of
numbers: integers, rational numbers, real numbers, and complex numbers.
For the most part the distinction isn’t much to worry about, but there
are times where one must, such as overflow with integers. (One can only
take the factorial of 20 with 64-bit integers, whereas on most
calculators a factorial of 69 can be taken, but not 70.) Julia
automatically assigns a type when it parses a value: a 1
will be an
integer, a 1.0
an floating point number. Rational numbers are made by
using two division symbols, 1//2
.
For many operations the type will be conserved, such as adding two
integers. For some operations, the type will be converted, such as
dividing two integer values. Mathematically, we know we can divide some
integers and still get an integer, but Julia
usually opts for the same
output for its functions (and division is also a function) based on the
type of the input, not the specific values of the input.
Scientific notation represents real numbers as $\pm a \cdot 10^b$, where
$b$ is an integer, and $a$ may be a real number in the range
$[1.0, 10)$. In Julia
such numbers are represented with an e
to
replace the 10, as with 1.2e3
which would be $1.2 \cdot 10^3$ (better
known as 1,230) or 3.2e-1
, which would be $3.2 \cdot 10^{-1}$ (which
is equal to $0.32$).
Take note that this $e$ is not the special base of the natural
logarithm, but is just notation indicating a power of $10$. To use this
notation, you must have a number immediately before the “e”, not a space
or an asterisk. That is, these are not correct: 1.2*e3
or
12 e 3
.
# Your commands go here