Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"


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 Mathematica in 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 Sinh 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 call 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 here— 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. 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. Now, 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” and 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 before 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” test 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. Hello and welcome to Wolfram Language tutorials—plotting functions. My name is Harry Calkins. I’ve been a trainer using Mathematica and the Wolfram Language for almost 20 years now and I’m going to guide you through this section on working with our plotting functions. Plotting functions are some of the first things people try but very often they never experiment and learn some of the more advanced things that you can do. Hopefully we are going to give you an idea of what some of those things are and how to use them as we go through this tutorial. The basic “Plot” function takes a minimum of two arguments— the expression to be plotted and the domain of the plot. here we’re plotting with respect to the variable “x”, as “x” goes from 0 to 10. Sometimes we would like to plot more than one function at the same time and to do this we put them in a list. This makes the first expression in the “Plot” function the thing to be plotted and again the domain of the plot is specified by the second argument. In plotting three-dimensional functions we have one slight variation from the unit variant situation. Here we have the sin(xy) and now we need two domain specifications— one for the “x” and one for the “y” variable. The same rules hold with three-dimensional plotting as with two-dimensional plotting. And if we wish to plot more than one function at a time, we put them in a list. Plotting data is handled in very much the same way except that the data itself specifies the domain of the plot. Here, we plot the values 1, 4, 9, 16, and 25. And we can see we get a sequence of points plotted at appropriate heights above the axis. Sometimes you want to plot an array of values and it depends upon the nature of the array to exactly how it’s handled. Here we have a collection of ordered pairs. This, of course, is going to give us a plot where the actual value in the ordered pairs determines the position of the points in the plot. A second function for plotting data points is the “ListLinePlot”. This joins the points by connecting them with straight lines. Sometimes when you are working with this, you may want to actually include the points and in the moment we will see how to do that. Here is an example of plotting an array of values where we would like to plot in three space. Let’s see what we’ve got here. Here we have a four by four array of values and the heights are determined by looking at the values here—this is the z value, if you have an x,y,z coordinate system— and the values are placed at the latest points: 1, 1, 1, 2, 1, 3, 2, 1, 2, 2, 2, 3, and so forth. You can think of this as constructing a little tent where you put poles each one of the latus points, cut the poles to the appropriate heights and throwing a blanket over it. That’s essentially what we’re doing here. You can also actually specify the points giving a collection of ordered triples. And if you wish to have rather than the surface represented the collection of points represented, you can switch over to “ListPointPlot” instead of “ListPlot3D”. “DateListPlot” automatically gets data or takes data and once it has the data it interprets the locations as dates and therefore the tick marks on your plot will appear as dates, which is of course exactly what you want. Here we are getting weather data about Chicago and we’re using the “DatePlus” feature here so this says, “2014 in January” and “2014 in December”. This produces a time series object, which can be fed directly to “DateListPlot”. We can also construct a bar chart of this. And this gives the heights of the bars as the temperatures for each one of the months. Notice that this is a dynamic feature and therefore when you mouse over it and hold your cursor over one of the bars you can actually see what the temperature at that time was. We can ask to include the “Mesh”— this is from that “DateListPot” that we saw just a little bit ago. Here we’ve gone in here put in the points actually indicating where that curve passes through. We can also for various other kinds of plots determine such things as the spacing between bars on a bat chart, the way the axis appears, the image size and so forth. We have set the bar spacing, the chart labels, the axis style, and so forth. Notice that we have labels that don’t seem to quite appear here and that can often be determined by just how opaque you make the features. So here we change this to .95 and now we can see the labels starting to show up down here. If we change this to a full value of 1 it will be essentially totally opaque. Now, to some extent, this is going to depend upon the font face you use and therefore you may find that will some font families the labels are somewhat faint and with other families they will appear as much more solid objects. There are wrappers that are available. This is particularly true with the chart functions. And so you can put wrappers around them to cause some of the labels to be rotated, to change the style, and so on. So here we are going to change the style—we’re going to set the size to be 14, the font color to be gray level 4, which is a fairly dark gray, and so forth and we’re going to map it across the months. So let’s see how that looks and again we’re going to have to scroll to see. Again, this comes out as appearing somewhat light so let’s change the opacity up to a .95 and now we see that we have the labels here as we had them before. Let’s now go in and rotate the one label for February and see how that appears. So again, when we look at this, we see that the labels are down here and that this one has been rotated. Now we can actually change the position and so forth, not just the rotation and things of that sort. So here we have used the same thing— let me change the opacity here so that we have darker labels that are easier to see— and we see that the labels are all rotated and they are all across the bottom here. I could put this as “above” and now the labels are above. If I put it at “top” we’ll see that they actually occur on the bars. So you can put it at the bottom, middle, top, above, and below using automatic settings. Here is an example of a more advanced effect. We’re going to bring an image into the program by evaluating this and assigning a name to it we can now work with this. Let’s get the image dimensions—image dimensions here are 648 by 431. That means that it is 648 pixels across, 431 pixels high. We’re going to actually use this construct bars in a bar chart. And so what we’re going to do is create image dimensions and then divide it by 12— so divide the 648 by 12—and divide the 431 by 1. So when we do this we see that we get 54 wide and 431 high. So now let’s take a look at those. We can actually split the bar up—the images up into bars— so here we see four bars and then another eight down here and you can see that we have just sliced through the image in a very uniform way. Well, we can actually assign a name to those and then put them into the bar chart. One of the interesting things is that the way the bar chart is designed, it will only take as much of the bar—as much of that slice we have from the image— as is required to represent the temperature at that time. What this means is the integrity of the total image is maintained. So we will be able to use this to create a more exciting chart then if we were to just use the plain orange bars as the default. So let’s get the image data here and we see that it comes out as an array of three tubals— these are RGB color values and one of the things that we can do is use “ReplaceAll” and replace the RGB values by RGB with .15 as a fourth argument. This becomes an alpha channel and when we do this we see that the image is maintained but it is very faint. So now we can go ahead and partition this up and once we have partitioned this up using the same method as we used before, we can now determine the maximum value of any one of these— that’s going to be 74.26—which, if I remember correctly takes place in July, and now let’s put all of this together in a bar chart and we see that we have the full image here because what we have done is taken the maximum value from the bar and used that in all twelve places. Why would we do that? Well, if we now take the original bar chart that we had and the new one and make sure that we have put a tool tip around the new one that has a delay in the thing so that we don’t see the values when we use it, this will allow us to combine the two bar charts that we have created so we have a full image in the background and the bars quite easily read in the foreground. Depending upon how much opacity or lack thereof that you use in the background and the intensity of your original image, you can get very nice contrasts and still have a visually continuous image for the entire display. Hello, and welcome to the Wolfram Language tutorials—programming. My name is Harry Calkins. I have been with Wolfram Research for almost 20 years now. Most of that time has been spent teaching people to use Mathematica and what we now call the Wolfram Language. I’m going to guiding you through this session here on creating a function where we will be able to feed in a data set and an image and we will be able to superimpose our bar chart on the image. The advantage here is that if I want to show temperatures in Chicago in a travel pamphlet or in something of that sort for the period of a year, I could take a nice image that is representative of the city, create the bar chart and use that same image as the background as we will see here. This will be a single function and it will take advantage of the built-in features and options of the Wolfram Language. We are going to create a function called the “PictorialBarChart”. The goal would be to produce a bar chart similar to what we see here. We have the image of Millennium Park in the city of Chicago, we have the months clearly laid out down here, we have the temperatures in degrees Fahrenheit indicated on the vertical scale and we see that the image is intense up to the height of the temperatures. But if you look carefully, you can see that the image is completed in the background so we have created a relatively quiet, blurred image in the background and an intense image in the foreground, which conveys the information that we are interested in communicating. So how would we approach a problem like this? Well the best way to program in any language and includes the Wolfram Language is to sit down and ask yourself, “what do you want to accomplish?” and “what do I need to have to make that work?” So need to have the input, syntax for the function, how are we going to present the thing to the user; we need to figure out in terms of actually working with the function how many bars we need to create, how big is the image that we have fed into it, we need take a look and see if there are any special options we might want to use and if there are how are we going to set those values and pass them into the function. We’re going to construct the bars. There’s going to be two sets— one set for the actual temperature bars and one for the background. But they need to be constructed in such a way that when they get superimposed, one on top of the other, that the image is continuous. We need to construct the foreground and background image. We need to create the user function and then we’re going to create some messages. So let’s get started. So here is going to be our first definition; we’re going to make bars. Now this is always a good way to do things in the Wolfram Language because what we’re going to do is make a small function that performs a particular task and we’ll use that in our final function. So here we’re going to feed in the image and the count— the number of bars that we need— and so we’re going to image partition this as would be expected. Another thing that we want to do is provide a method of introducing opacity and blur into our image so that we can create a background image that doesn’t overpower the bars and that can be done by using an alpha channel, put in the image, put in the opacity that we would like, and then put in a blur factor. The opacity and blur will be arguments to this function so that users can set their own levels wherever it is necessary to do so. Now let’s construct the layers. Well the first thing we’re going to do is realize that we can make more than one function with the same name—more than one version of the same function. So we’re going to create a background function, we’re feeding in the data, the bars, the maximum value of all of the bars, and then we’re going to put in “BG” for background and then we’re going to put in options where we’re going to take the “OptionsPattern” for pictorial bar chart. And the bar chart that this is going to create is going to be a bar chart with a tool tip, with a tool tip delay of infinity to keep the tool tip from popping up, the chart elements are going to be bars, there’s an option for the bar spacing in the bar chart so we’ll set that to zero— automatic will go to zero—and we’re going to put in filter rules for bringing in the options that work in BarChart, not in our function, but in the actual Mathematica or Wolfram Language function. Similarly, we will do the same thing for the foreground. So we change the “BG” to “FG” to represent foreground and what’s going to happen is that we’re going to now use the heights of the bars as the values of the data set. And then we’re going to set the options for this and this is a quick and easy way to do that. We’re going to join the special options that we are going to use and process the background—that is the opacity and blur— and we are going to use the values for the bars spacing as part of the plot so we’ll make sure that we have that in here. We need to make sure that the bar spacing is changed from the default value of the bar chart. So here is our function— “PictorialBarChart”— we put in the data list, an image, and then we put in the options. And we’re going to create a number of localized variables here using the scoping function, “Block”. We can see here that these are all going to be localized values. We choose the maximum and multiply it by 1.02 and then we’re going to process our image here. We’re going to create the background for the thing. We’re going to create the foreground. We’ve going to make the layers here and then we’re going to show the two things together. So that’s what happens in our function. Notice that we have used the functions we defined previously in the process “makeBars”, “makeBars”, “makeLayer”, “makeLayer” and so forth. So now that we have written our function, the next thing that we want to do is tell users how to use this. So, I write a usage message and I put in as the first part of the usage message a template because the program is automatically set up to recognize that as a template. Now here, once I have done that, if I go in and then say “? PictorialBarChart”, it gives me a usage message of the sort that I am used to seeing when I use the program. And furthermore, if I start typing in—so if I do this— you’ll notice that right below is the auto-completion and by hitting that I automatically have picked up the auto-completion. So now if I don’t move my cursor and include the “Shift” key along with the “K” key or normally if you don’t have autocomplete set what you would do is “Control” and “K” and it would give you the auto-completion, toss in the “Shift” “Control” and “K”—“shift” “command” “k” if you’re on a Macintosh— and it gives me the usage message and if I accept that it should autocomplete here. So now we’re ready to test our function. We have the weather for Chicago in 2014, so here’s the image we are going to use and now we are going to check the weather data again to make sure that we have everything ready. We have a time series here for this so we’re going to put in the weather values—that’s our dataset. We have to put values in here because we need to get the numbers, the way we have designed the bar chart here. It is possible to make it work with the weather data and the time series that comes back but for now we are going to just go in and get the values, which means we’re going to get the height values for the bars. Here’s the image and here are the chart labels and we’re going to place those below so we do this. We define two variations of the “MakeLayer” function with one for the background and one for the foreground. Here we’re going to put in the data and a bars list, which is going to give us the bars as having created from the image as we saw a moment ago. And we’re going to create the background one first so we’re going to take the one where we have made the image fairly pale and blurred. And this is going to give us all of the bars the same heights so it is going to give us a full image. Alright, so now let’s test our function. It’s important to always test your function before you give it to somebody so that you make sure that all of the pieces are in place. So we begin by introducing the image, by the getting the weather data for Chicago, and now we are going to go ahead and evaluate the thing. And we see here that we get a nicely formed image with the bars here. Because we have set the tool tip delay in the background to be infinite, as we mouse over the bars in the front here what we get here are the actual temperatures as we go up and as we mouse over the area in the background we do not see anything— it’s only when we actually have our tool tip over one of bars. Well, we use this to construct this function so we should probably try it with a different image. If you want to see actually building this function you want to look at the tutorial on working with visualization. So here we have now weather data in San Francisco. Let’s go ahead and load that in; it takes a few seconds because we have to communicate with our website to get that information and bring it in. Now we have it. Now let’s go ahead and introduce an image of San Francisco— the famous cable cars running up and down the hills in San Francisco. Now let’s go ahead and put this in to our function and here we see the cable cars and the bars indicating the temperatures over the months and that little extra space that we introduced by using the max value in the function. And again, up here in the background area, we do not see the tool tips interrupting us. In the foreground it actually gives the Fahrenheit temperatures and so on. This, of course, can be made larger by introducing image size and so forth because all of the options that work with the bar chart continue to work with the chart that we have just created. z Well, as a little challenge to you, you can download a copy of this notebook and you will notice if you play with this a little bit that there are some limitations. For example, when we got the weather data it gave it to us as a time series but we had to use time series with the argument values in order to get a list of the numerical values to create the bar chart. It is possible to set up the definition of the function so that it will do that automatically—you don’t need to do that. In other words, if the head of the expression, given as the first argument to this is actually “TimeSeries”, it will automatically replace that with time series values and then you can use the definition that we have already created. There are some other things we offer as a challenge to you— the possibility of setting this up with times series or temporal data. Another thing that we changed in here, we had the plot range in setting the size of the foreground plot and we can make various adjustments in that. We already have an example of doing that a little bit in there by multiplying by the 1.02 but one can extend this if one wanted to have a more dramatic spacing in between the bars and the background. Well I hope this has helped you understand how you might create a function of your own to do some special task. Thank you for joining us today. This is focusing on report generation, which is a feature added in Mathematica 10. And it’s one of many features that were added to our data science tools that were built in to Mathematica 10 and I think this is a really great tool— it’s very straightforward to use and we’re going to probably very quickly get to go over that here. The core tool for report generation in Mathematica is the template. The core tool for report generation in Mathematica is the template. What you see here is a blank template that we have available to us. And we have several tools of what we can build—we could put in slots, expressions, repeating blocks, and cell behavior. Now, I have an example that is what I want to build, so I’ll open that up. Here we are going to make a very basic template that is going to have a single text cell saying, “hello” and then the recipient of the “hello”—recipient will be represented by a slot so I will type “Alt 7” to generate a text cell, I will type “hello”, and then I will click our “slot” button and I will call it “recipient” and the default value is going to be “no-one” if we don’t provide one. And finally I am going to put an exclamation point at the end so that we can do a very traditional hello world example. So let’s move that over and now I can fill out the document with the “GenerateDocument” function. We’ve got this “templateNo” variable defined form when we created the template and we are going to use this association, which is a data structure that we haven’t talked about but is another feature new in Mathematica 10, and we are going to say that the recipient is going to be replaced with “world”. And so I will execute this notebook. And as you can see, the result is a notebook in Mathematica where the recipient field was replaced with “world”. In our next example, we’re going to look at the ability to use expressions. So, here again I’m going to generate a template and let me bring up what our target is. Here we have the notebook that we’re going to be working in and this is what we are going to be creating. So I’m going to make a text cell and we’re going to start this example “integrates” and then we’re going to have an expression put in and “expr”, which the default here is going to be “x”. And then I’m going to give myself a couple lines and then we’re going to include an expression to be evaluated and “integrate” and then we’re going to put our slot back in and it give “expr” again and put our default value in and we’re going to integrate with respect to “x”. Now we’ve built our template for this case. So once we have that template built I will be able to, again, use that “GenerateDocument” function as you see here replacing in the for expression with “Cos(x)”. And when we see the result we can see that “this example integrate cos(x)” that “Cos(x)” was put in for the expression and in both places the integrate command was evaluated and we get back “Sin(x)”. The next tool we’re going to look is a cell behavior tool. And this is so that you can put in some commands that you want to be evaluated during the course of building the notebook but you do not want them to show up in the final document that you are generating. So let me open a template again and I’m going to, again, pull up my target notebook. So here I’ve got the template that I’m about to work with and I’ve got the target. First thing I’m going to do is put in a little tag to show that this is initialization. And then I’m going to define our function. And here we’re going to take an integrand and we’re going to integrate that integrand with respects to “x”. Now, what I’m going to do is select these cells and I’m going to use the “cell behavior” button and we’re going to do an evaluate and delete so that will completely remove these cells after they’ve evaluated. Now I’m going to put in a heading so we can have a non-hidden section. And underneath the non-hidden section we are going to have a text cell that says that “this example integrates our expression” and the default value for the expression will be “x” and then we’ll have an input cell where we evaluate “f” of our expression. And the default value in this case is going to be “x” again and we’ll put a closed square bracket on here. So now when I go to our notebook, we’re going to evaluate the “GenerateDocument” section, and we see that “f’ of “x” ended up defined and the above section was removed though. Now I want to change one more thing about our template because, as you can see, “f[cos[x]]” was still sitting there so I want to evaluate that section. So we want to make sure that that “f[cos[x]]” is going to be removed. I’m going to select that cell and choose its “cell behavior” to be “evaluate and delete”. And then when we run the “GenerateDocument” we find that the evaluation is remaining but the term that generated “sin[x]” has been removed. So now we’ve got a nice, clean report just the way we want it. The last tool that we want to talk about with respects to templates and report generation is the “RepeatingBlock”, so that you can have an arbitrary number of values that you might feed into the template in such a case. Let’s take a look at a base template and let’s take a look at our target in this case. Here we have our target of what we’re coming up with. First off we’re going our to include an initialization section like we did before. So I will use “Alt 6” to label this, “Initialization”. And then we will define our function again. And again this is going to integrate the parameter that is passed to it. And we’re going to mark those two cells as having “cell behavior” as “evaluate and delete”. Then we are going to have an integral that we take, so we’re going to take an integral of and we’re going to pass an expression in here, and, again, the default value is going to be “x”. And then we’ll have a test cell saying, “the integral is”, and then finally an expression—f of our slot— expression passing value “x”, closing it and we’re going to want to change the behavior there to “evaluate and delete”. And then I want to create a repeating block so select the section I want to repeat. So I’ve selected the “integral of expression” cell all the way through the actual call to the function “f”. We’re going to click on the “repeating block” and we are going to give a template variable whose repetition is going to be called, "integrands". So now we’ve got this repeating block. Now let’s take a look at how we’re going to use it. Here, I’ve got, as we pass to “GenerateDocument”, an association where “integrands” points at a list of associations and in there that list has expression being assigned “sin(x)”, expression being assigned “x^4”, and expression being assigned the exponential of x. So let’s evaluate that and we get back our report. Here we find report where each of the different sections was covered— we’ve got integral of sin(x) coming out to the negative cos(x), the integral of x to the fourth coming out as x to the fifth over five, and the integral of the exponential function coming back as the exponential function. So go ahead and close out of the notebooks that were generated there. Finally, we want to show how you can use all of these tools, these report generation tools, with everything else that we’ve done in our talk today. I’m not going to build the following template; I’m just going to show you what we have here. So here what we have is our final template and all of this functionality I’ve put into initialization above this is all the functionality that you were shown previously in the third section about creating functions that have error messages and have lots of options and lots of possibilities. The only thing that I have added is a “Wrapper” function that simplifies the data that we are feeding in, so we’re only going to feed in the city name and an image taken from that city. Then we look further down, we’re going to have a “RepeatingBlock” and it’s going to go over cities and there’s going to be a particular city, we’re going to use our “WeatherData” function to get the current temperature in that city, we’re going to get the current wind speed and wind direction in that city, we’re also going to find the humidity level in that city. Finally, though, we’re going to use our “Wrapper” function and we’re going to generate the plots that you saw before. So if we go back here, to our “GenerateDocument” function, I’ve got my weather notebook that we’re referring to and “Cities” is going to point to this list. And that list has several different associations underneath it. The first one is going to be San Francisco and the image in this case is going to refer to an image of the streetcars in San Francisco. Second is Chicago and we have a picture of the Bean at Chicago. And finally we have Cairo and an image of the pyramids at Cairo. And so I evaluate this. And this is going to take just a little bit longer to evaluate because we are trying to get data off of our weather data servers. Alright, and after we’ve waited a suitable amount of time for all the data to come in, we get our final report. And I believe this, to me, looks really nice. We have sections divided up by city, we have current temperature, the current wind speed, and the current humidity level. And we get this nice image that Harry showed you how to build, in this case showing the average temperature in San Francisco are pretty much gorgeous year-round. Chicago a little bit less so, it gets a little cold in the winter. And then finally at Cairo where we get relatively warm temperatures year-round.

Video Details

Duration: 1 hour, 1 minute and 23 seconds
Language: English
License: Dotsub - Standard License
Genre: None
Views: 42
Posted by: wolfram on Apr 15, 2015


Caption and Translate

    Sign In/Register for Dotsub to translate this video.