2.1: Numbers
- Page ID
- 53704
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Solving Scientific Computing problems ultimately boils down to manipulating data and at the most basic is that of strings and numbers. We begin with understanding these data types and how to store values in them.
We also show some of julia syntax, which looks like other languages (like python). Hopefully you have some basic knowledge of computing, but no assumption of any particular language is necessary.
Numbers
Not surprisingly, the most important data type in scientific computing–at least at the most atomic level is numbers. Numbers in computation mimic those in mathematics with some important differences. Julia like most computing languages have two main number types, integers and floating points. Julia’s integers and much like mathematical integers in that they store numbers like \(0, 10, -400\). Floating-point numbers generally are approximations to real or decimal numbers and more details will be covered in Chapter ??.
Julia also has a rational data type for numbers commonly though of as fractions like \(-\frac{2}{3}\) or \(\frac{22}{7}\). Recall that typically \(i\) represents \(\sqrt{-1}\), the base imaginary number. Complex numbers like \(2+3i\) are also native to julia. We will explore both rational and complex numbers in greater depth in Chapter ??.
Assignment Statements and Variables
Anything can be stored as a variable using the single equal sign like
This is an assignment operator, which creates the number 6 and stores it under the namex
.
And now that the variable is stored, we can use it in calculations. For example
Variables in julia, much like other languages are primarily sequences of alphanumeric characters as well as an underscore . Primarily, a variable needs to start with a alphabetic character or and after the first character can contain numbers.
Julia also allows many unicode symbols in variable names, however not everything. For example, all of the greek letters are allowed, so \(\alpha=45\) is valid.
To get a greek letter in Jupyter or the REPL, type \alpha
, hit the TAB key and it will be turned into an \(\alpha\).
Storing Variables in a Virtual Whiteboard
The details of storing variables in computer hardware isn’t necessary, however, thinking of storing as writing variables and values on a whiteboard is a helpful paradigm. Imagine a whiteboard with a column of variable names and a column of values. For example, if we have
then you can think of the whiteboard looking like:
variable | value |
x | 6 |
y | -1 |
z | 8.5 |
If we evaluate any expression containing any of these variables, then the value looked up substituted into the expression. For example,
looks up the value of y (which is -1) and substitutes that value in and multiplies the result by 2. As you can see the result is -2.
If we change one of the values, like
this means that the right hand side is evaluated by looking up the value of y
and the result is 4. Then the 4 is placed into the whiteboard, which will now look like:
variable | value |
x | 6 |
y | 4 |
z | 8.5 |
If you are thinking of how a piece of code works, often you will need to get to the point of writing down a version of the whiteboard.
Strings
In many Scientific Computing fields, such as Data Science, strings arise often and it is important to understand some of the basics of them. In Julia, a string is a sequence of characters surrounded by ""
(double quotes). For example:
and if you enter
which shows that it is a String
. Section 1.2 will go into more details about data types in Julia. The individual parts of the string are called characters, which are of type Char
and are by default Unicode Characters (which will we see are super helpful). A few other helpful things about strings are
-
The length of a string is found using the length command.
length(str)
returns 16. -
To access the first element of the string, type
first(str)
, the last is found bylast(str)
and the 3rd character for example isstr[3].
In julia, string indexing starts at 1 -
To turn other data types into string, use the
string
function. For example
and again note that the double quotes indicate that this is a string.
String Operations
A common string operations is concatenation, or the merging of two strings. Consider
We can concatenate in two ways, with the *
operator symbol or the string
function. Both
returns the same string concatenation. I find the second option clearer in that *
is an odd choice for string concatenation. Many languages including java and ecmascript (javascript) use +
instead for string concatenation, which I think intuitively makes more sense.
Another cute operation for strings is the caret operation. This could be helpful, however a not-so-helpful example is
concatenates the string three times. Other important functions related to strings can be found at JULIA DOCUMENTATION ON STRINGS
String Interpolation
Mixing strings and other variables together is often needed and we can use the methods of concatenation as above. However, there is a better way. Let's use the value of x
that we stored above and insert it at the end of "The value of x is "
. The following will do this
There is actually another way to do string concatenation if you use this method. Using the variables stored in str1
and str2
, then
will also concatenate the strings.
We can also go beyond just looking up strings in that we can execute code as well. Let's say that we have the variable
If we want a string with its square, we can enter:
where you should notice that the code n^2
is executed since that code is within the $().
Exercise
Use the volume of a sphere \(\frac{4}{3}\pi r^3\), to calculate the volume and produce a string. For example, store r=3 and then return the result
"The volume of a sphere of radius 3 is 113.0973355292"
where 3 and the volume is generated with string interpolation.
Expressions
An expression is a combination of variables, data elements (like numbers and strings), operations (like + or *) and functions (like ). We’ve seen a number of expressions throughout this chapter so far like
In short, writing things in julia will consist of writing expressions (and slightly more complicated structures).
Operator Precedence
When we type out an expression like 11+2*(4+3)^3
, it is important to understand the order in which operators are performed. For mathematics, the PEMDAS pnemonic is helpful to rememember in that the order is:
-
Parentheses: The expression inside the ( ) are done first. For the example above, the
4+3
is the first operation done. -
Exponentials: The ^ is done next. Raise the 7 from above to the power of 3 resulting in
343
. -
Multiplication and Division: In this example, the
2*(343)
is done next -
Addition and Subtraction: Lastly add 11 to the result and the result is
697
.
In any computing language, there are other operators as well and there is order to that precedence, so we will see that there are other things to think about. For example, the assignment operator, has the lowest precedence. That is when assigning something to a variable, all calculations are done on the right side of the = before the assignment.
Details on all this can be found on the JULIA DOCUMENTATION ON OPERATOR PRECEDENCE
Comments
A comment in computer code is sequences of characters which are ignored. The purpose of a comment is to alert a human on what is going on. You may have been told to write comments so that someone else who reads your code understands what you are doing. However, I have found that the person mostly like to read your code is you at a later date. You should add comments for yourself.
In julia, a comment is anything to the right of a , pound sign or hash tag. For example:
Both lines 1 and 3 have comments. On line 1, the entire line is ignore since the line starts with #
. On line 3, everything after the 2 (the power) is ignored. Also, notice that there are two hash tags on line 1 and 1 on line 3. This is simply different style. Since anything after a single is a comment, everything after the first one is ignored.