# 01_WL_Tutorials_MathematicaIntro_CoursePreview

0 (0 Likes / 0 Dislikes)

Hello, my name is Jason Grigsby.
This section is based on our Introduction to Mathematica course
and the focus is going to be on core language features.
In this section I am going to be aimed primarily at getting you to understand
the way replacement happens, the use of rules,
and pattern matching inside of Mathematica and this is one of our key features.
So the first time you use Mathematica you are going to get a welcome screen
and I’m going to open up Mathematica again so we can see that welcome screen and get to take a look.
And you have this welcome here and you get a button and you can hit it to generate a new document.
Now, what you’ll see is a blank screen and you’ll have this horizontal cursor
and whenever you see this horizontal cursor that means that it’s not in a cell in the notebook.
But if you just start typing—so for instance,
if I type “2 + 2”—Mathematica’s automatically going to assume
that this is in input cell
and with that input cell I can then,
as long as my cursor is in that cell,
I can hold down “shift” and I can hit “enter” and that cell will execute.
So now everyone should be able to execute a basic Mathematica command,
executing two plus two.
Mathematica notebooks have a large variety of cell types
and as you can see we’re actually working in a Mathematica notebook here.
And you can see the cell structure showing up along the side.
At the top we’ve got a chapter selection,
we have text cells, we’ve got bullet points—
all of these different styles can be made in Mathematica
and there are a variety ways to generate those different cells types.
Now, starting always with this horizontal cursor,
I can go up to the “Format” menu of Mathematica
and select “Style” and I can enter a style that I want.
So now I can have my chapter cell right here.
Another option that I can do is I can right click and there is an “insert new cell” option
and I could pick out the cell type that I want so I can put in my section heading
and then finally a number of the cell types are mapped to keyboard shortcuts—
Alt 1 through Alt 9.
Alt 1 is always a title cell
and I believe Alt 9 in this case is input.
Am I right?
Yes.
Alt 9 is always input, in this case.
These are kind of the first steps you need to start interacting with Mathematicain the notebook format
Next up we want to cover some of the basic computations
—basic calculations that you’ll do in Mathematica.
As mentioned above,
whenever you have an input cell and your cursor is in that input cell you can
hold down “shift” and hit “enter”.
So here we calculate that two plus two equals four.
You can do all kinds of things in Mathematica
and we are not possibly going to touch on all the functionality that is built in
but as a few common examples that Mathematica is known for we can execute this plot command
and generate a plot of the Sinc function and we can solve an equation,
coming up with in this case three different solutions to this equation for possible values for “y”
and we could even create interactive tools.
So here generating a contour plot that is determining the potential lines,
lines of constant potential, between two point charges.
We can address what those two charges are
and move them around each other.
So we should now be comfortable executing some basic commands in Mathematica
but we really want to focus on what is the structure of Mathematica
and here I want to mention that the Wolfram Language,
the language that powers Mathematica, is a functional language.
So here we have a plot function and we know that it is a function because it has these square braces
for the arguments that are being passed to the function
and we have two subexpressions—
sine(x) and x of the list, x, 0 and 10.
So here in this case Sin(x) is defining the function to be plotted
and x, 0, 10 is finding the range over which to plot.
But these subexpressions are themselves Mathematica expressions.
We will often refer to subexpressions when we want to refer to an individual piece of code so in this example
I might say, “the subexpression sin(x)”.
The last item that we talked about in the previous slide was a list
and a list is what I would call a very special type of function in
Mathematica or the Wolfram Language
The braces here that you see
—these curly braces—
are a special notation because lists are so common in Mathematica.
So technically,
a list is the list function.
The bracket notation in this case is simply a shorthand so once I have a list,
and here I am assigning that list to a symbol “a”,
I can access individual elements of the list with the “Part” function.
So here we’ve picked out the third element of our list
“a” and that turns out to be 3.4
There’s a shorthand for “Part”
that’s the double square brace notation that you see right
so “a” open square brace,
open square brace, “4”, close square brace, close square brace will pick the fourth element of “a”.
And you’re going to see me execute a “Clear” function
that is a common issue in the Wolfram Language that if you work in it for a long time
and you aren’t being very careful about cleaning up your definitions you might run into what we often call
“definition leak” further on
we have to get down Now, when we talk about Mathematica expressions eventually
we have to get down to an item that is the base out of which we build other expressions
and we refer to those base items as “atoms”.
So here I have created a list of atoms.
We can use the “Head” function to identity what those items are.
So here I’m going to map the “Head” function to each element
and we find that “b”—this is just a symbol—that the number 2 is an integer,
the number 2.3 is a real, the number four divided by five is a rational number,
that 2 plus 3i is a complex number and the string “hello” is registered as a string.
Now,
one should be aware that when we classify numbers—integer or real, rational or complex
—this isn’t strictly a mathematical classification;
this is referring to the underlying representation in Mathematica.
It is true that all integers are members of the reals but in Mathematica
we represent an integer differently than we represent more general reals.
We can always test if something is atomic with the “AtomQ” function as we see here.
So again,
I’m going to clear out my list
and make sure I don’t have any definition leak further on down.
So I wanted to go over lists because we’re going to start looking at pattern matching
and the “Cases” function, acting on a list,
is probably the best tool to get start on pattern matching.
The first item I want to introduce to you is the “Blank” function.
The “Blank” function stands in for any single item
and so we’re going to find all the cases in this list that are single items.
And what you’ll see is that we get back the same list.
And the reason for that is that “1” is a single item,
“1.” is a single item,
“1. + I” is considered a single item, “1/2” is considered a single item,
and the list of b and c is considered a single item.
Even though there are two items in the list,
the list itself is a single item.
there’s a shorthand for the “Blank” function,
which is a single underscore,
and we’re going to get back the same result as you see here
One thing we can do once we have the “Blank” function
is we can specify what kind of head we are looking for.
So, in this case,
we are going to be looking for an integer
and we represent that by an underscore followed by the label “integer”
and that’ll look through the whole list of items
and look for anything that has the head of “integer”.
In this case,
we’re going to return a single element—1.
Not one point, even though that one is an integer,
the representation is, again, of a real.
And “1. + I” is not an integer, “1/2” is not an integer,
the list is an integer, a symbol is not an integer, and a string is not an integer.
We can search for any other kind of head that we are interested in,
so we can search for “symbol” and we get back the raw symbol.
One thing that you’ll notice is that b and c were not selected.
The reason for this is that by default,
the “Cases” function is only going to look at what we would refer to as one level of depth.
So it’s going to look for anything in here and this sublist is just going to have the head of “list”.
We can specify a depth as a third argument to “Cases” and we here do just that.
When we specify a number without putting any braces around it that means we search down to depth two.
But, in including previous levels, so “b” was identified as a symbol, “c” was identified as a symbol
and then the word “symbol” was identified as a symbol.
And those three were all returned.
If I wanted to change this and only look at depth two,
I would put my “2” in a list
and then it would only find “b” and “c” because “symbol” is sitting at depth one instead.
I could look for arbitrary objects—they don’t have to be atoms
I could look for a list,
for instance, and I get back the list “{b,c}”.
lastly, we don’t have to look for just atoms,
we can look for more complex expressions as well.
So here we’re looking for anything with the head of a list.
And when I execute it, as you can see, we get back the list that contains the elements “b” and “c”.
We can go far more complex though when we are looking for patterns—
we don’t have to necessarily look for a single item,
we could look for sets of items, we could look for a list of items.
And so here we are going to go over some of the many ways we can look for more complex patterns.
In this case, I’ve got a list of sublists.
The “{2,hello,3,4}”, “{5,goodbye,6,7}”, “{again,8,9}”, “{10,before,11}”
and then here I am specifying the pattern I want to find.
This is a pattern where I have one item, then a string, and then an item.
So we’re going to execute this and we’re going to find that only one element is returned and that is the “{10,before,11}”.
“10” counts as a single item, “before” is a string, and “11” counts as a single item.
We don’t take the other cases so, for instance, “2”—that’s a single item
followed by “hello”, which is a string, but then “3” and “4” are not a single item at the end
so that was rejected. Similarly,
having the “5” the “6” at the start of this list rejected it from fulfilling our pattern.
Then having nothing before our string in the third case
rejects it from being associated with this pattern—being agreeing with this pattern.
If we want to increase the flexibility we actually have further tools that we can use
So in this next case we are going to use the “BlankSequence”,
which is represented by a double underscore and that means one or more items.
So, if I apply that here we are also going to include the “{5,goodbye,6,7}”
because “5” and “6” count as one or more items for the string.
Again, it doesn’t take the “{2,hello,3,4}”
because we allowed for only a single item at the end and it’s not taking “{again,8,9}”
because there are zero items before the string shows up in that sublist.
If I put double underscores on both the beginning and the end of the string pattern,
we’ll find that we are also now picking up the “{2,hello,3,4}” list.
Finally, we have the “BlankNullSequence”.
“BlankNullSequence” means zero or more items and it’s represented by a triple underscore.
So let’s see how this execution comes out.
We get the first two items from our list because they both have one or more
items on both in front of or behind the string in their ordering but we also
pick up “{again,8,9}” because nothing being before the string counts as zero or more items,
which agrees with the “BlankNullSequence”.
Patterns can fulfill a lot of different roles in Mathematica
and pattern matching can be used in many different ways
and we are going to go over some of those ways now.
So first off, we are going to apply a condition function.
So here, let’s take a look at the full form.
Here we get a pattern of “a” being blank.
What we do with this is we are setting a label up and this label “a” can now be a stand in later on.
So let’s take a look at this new “Cases” argument.
Here we have a list of items—we should recognize that from before—
and we say that we are looking for “q” and this slash semicolon refers to the condition
and you can read this off as “any q such that the square of q is greater than 0.5”.
So when I execute this I get “1” and “1.” back,
so “1” squared is 1 and that is going to be measured as greater than 0.5; “1.”
squared is going to be measured as “1.” and that, again, is going to be greater than 0.5. “1/2” squared
would come out as “1/4” and that’s not going to be greater than 0.5.
A list cannot be compared to a number so it was rejected; a symbol cannot be compared
and a string cannot be compared to a number so they were rejected as well.
Another tool that operates in a very similar manner is the “PatternTest”.
Here we are going to see an example of the “PatternTest” where we use a blank,
a question mark, and the “NumberQ” function.
The question mark is referring to the “PatternTest” function
and it’s followed by the “NumberQ” function.
You can use any function you like with the “PatternTest” so long as that function return true or false.
And we’re going to accept in this case only those items that return “true” from “NumberQ”.
So 1, 2, 3.5, and 4 are all numbers and they returned “true”
and so they were included in our “Cases” statement.
“x” and “y” in this case were symbols and are not considered numbers
and were returned false and thus were not selected.
One of the primary uses for patterns in Mathematica is in the definitions of functions.
So here we’re going to take a look at one key example.
Here we’re going to define a function, “h”.
“h” is going to have a single argument—this “x_” means it can take anything.
And the result is going to be that we square what we pass into it.
So I execute that line of code and now we have this definition for “h”.
The head of the function is “h”, and it’s going to be passed a single argument.
Here we are going to pass the number two and we are going to find that it gets squared
and comes back as “x” squared and that works rather well. Unfortunately,
we can pass anything we want in for “x”. So in this case we find that “hello” is being squared
And we can even find that our graphic gets squared.
Let me make that a little bit bigger for everyone to see.
So, it can be a little odd trying to take the square of a graphic
and Mathematica certainly doesn’t know a way to evaluate that further.
Here, we’re going to start using patterns for type specification.
Let’s take a look at this other example—we’re going to define a function as “h1”,
which will take a single argument but that argument must have a head of real
and we’re going to square that argument.
So now when I apply “h1” to the string “goodbye”,
it’s not going to evaluate.
The reason is that there is no definition of “h1” that accepts a string as its argument—it must accept a real.
So now when I pass “2.3”, 2.3 squares successfully
Unfortunately, because we have specified reals,
we have not included integers or complex numbers or any other kind of number—
only reals being accepted.
So, if we want to include all possible numeric types,
we’re going to make use of our “PatternTest”. So, the definition of “h2” here—
we’re going to take any value “x” with the “PatternTest” that the “NumberQ” returns true
so that we get a numeric value back. So, now we can take “h2” of 2.3,
and we can take “h2” of 2, we can take “h2” of i
and we can take “h2” of the string “my name is Jason”
and only in the last case does it not evaluate because a string is not considered numeric.
Another very useful application of patterns is in replacement.
So, we might be concerned with data that has been entered by various people
and it might be entered in different ways so we have several numbers but then we end up
with some people wrote “N/A” when data was not available,
some people wrote in “NA” when data was not available.
So this might get assigned to our data symbol in that list
and we are going to use a function called “ReplaceAll” and that is represented by “/.”
and we are going to say any “a”—such that “a” is a string—is going to be replaced with “0.”
So now we have successfully replaced our strings with zeros going on in here.
Another way to accomplish this is to do a pattern that incorporates the whole list.
In this case, we are going to look for any number of items
—“a”—followed by a string, followed by any number of items—“c”—and replace it with just “a” and “c”.
And you’ll note in this case that we still have the “NA” hanging around
and the reason for that is that this rule was only applied once
It identified the numbers 1.234, 3.768, 2. as being “a”, any number of items before a string, “
N/A” as a single string that was filled in for “b”, and “c” was identified as “NA”, as we see above.
This rule was applied once and “a” and “c” got put back together
and we were left with the “NA” possibility.
To address this kind of issue, there is the “ReplaceRepeated” function.
Now it’s represented by a “//.”
and it will apply the rules available to it as many times until there is no further change.
So in this case,
we first applied the rule once and got back the list that had “NA” at the end.
But then the rule was applied again and the “NA”
was identified as a string and removed once again.
So, in this case, “ReplaceRepeated”,
because it was able to apply the rule multiple times was able to identify all the strings in there.
One should be exceptionally careful with the “ReplaceRepeated” function—it is very possible
and I have many times myself made the mistake of using “ReplaceRepeated”
with a rule that would never stop making a change to my list
and it never ended so I ended up with a computer that was constantly chugging.