WEBVTT computer-science/introduction-to-ruby/mui
00:00:00.000 --> 00:00:02.000
Welcome back to Educator.com.
00:00:02.000 --> 00:00:10.000
Today's lesson is on math operations; this is the second part.
00:00:10.000 --> 00:00:19.000
For today's lesson, we are going to go over five pieces that are part of math operations.
00:00:19.000 --> 00:00:23.000
The first one we are going to look at is the Math module.
00:00:23.000 --> 00:00:38.000
This has quite a bit of operations; most of the operations are related to the Math module, so I would give this a star.
00:00:38.000 --> 00:00:45.000
After that, we are going to look at some of the math types.
00:00:45.000 --> 00:00:53.000
One is the rational numbers, and after we look at rational numbers, we're going to look at complex numbers.
00:00:53.000 --> 00:01:12.000
Then, prime numbers, and the last one we're going to look at is matrices, since they do have an object to create and manipulate matrices in Ruby.
00:01:12.000 --> 00:01:17.000
First, let's look at the Math module.
00:01:17.000 --> 00:01:29.000
This is a module that you can include in other objects; you can also use it by itself.
00:01:29.000 --> 00:01:35.000
It has two constants as part of it: one is π, and e.
00:01:35.000 --> 00:02:09.000
To call it, you just call the Math::π function, and the same with the e: you can just call it with Math::e.
00:02:09.000 --> 00:02:18.000
The M is uppercase, and the three letters after it are lowercase.
00:02:18.000 --> 00:02:23.000
That will get those two constant values.
00:02:23.000 --> 00:02:32.000
The Math module contains a lot of methods for trigonometry and logarithms.
00:02:32.000 --> 00:02:44.000
It can be invoked through the Math name space or included into scope.
00:02:44.000 --> 00:02:47.000
Let's look at some of the methods.
00:02:47.000 --> 00:02:58.000
The first one is atan2; it takes two arguments--in this case, we're giving it an x and a y.
00:02:58.000 --> 00:03:08.000
This calculates the arc tangent, given the y and the x, which are these two values.
00:03:08.000 --> 00:03:19.000
The next one is cosine; it uses the method cos; it takes one argument; and it calculates the cosine of x.
00:03:19.000 --> 00:03:31.000
For our example here, all you do is to call the Math module directly; then, do the period, and call the method cos.
00:03:31.000 --> 00:03:39.000
In this case, our argument is 0, and that comes up with the cosine 0, and the value is 0.
00:03:39.000 --> 00:03:44.000
The next method we'll look at is this exponential function.
00:03:44.000 --> 00:04:01.000
It uses exp, and this is e raised to the power of x, so it is equivalent to e^x
00:04:01.000 --> 00:04:22.000
For example, we have here the exponential to 1; e to the power of 1 is 2.7182, and it just keeps going down--81828459045.
00:04:22.000 --> 00:04:34.000
We do call Math here, but you don't have to; you can include the Math module, and it will be part of your scope, and you can just call the function.
00:04:34.000 --> 00:04:39.000
Let me show you how to do that.
00:04:39.000 --> 00:04:52.000
For example, first, I'm going to do include math; and now I'm already in the Math scope, so I just need to pass the function in.
00:04:52.000 --> 00:05:06.000
For example, before, I did math, log(1) and it gives me the value for the logarithm 1; I can just call log(1) now, and it does the exact same thing--since I included that Math module into my scope.
00:05:06.000 --> 00:05:23.000
Also, I can call the new function we learned, the exponential function, to the power of 1, and I can call cosine and pass in 0, and it gives me a value of 1.0.
00:05:23.000 --> 00:05:38.000
Actually, if that is the case, this is the wrong one; this is 1.0, as we just saw in that terminal.
00:05:38.000 --> 00:05:51.000
The next one, as we just saw--we just looked at the logarithm function--you call log, and you pass the parameter x; this calculates the natural logarithm of x.
00:05:51.000 --> 00:05:57.000
As we did in the terminal, math.log(1); you get 0.0.
00:05:57.000 --> 00:06:09.000
You can also call the constant; in this case, we will do the constant for the e, math.e, and we pass the logarithm of that.
00:06:09.000 --> 00:06:19.000
What is the natural logarithm of that value?--we get 2.7182, and it goes all the way down.
00:06:19.000 --> 00:06:36.000
This logarithm function--we can actually pass two arguments in it; by default, it's going to do it as a base 10, but you can specify a second argument.
00:06:36.000 --> 00:06:41.000
You can tell it specifically what base you want it to do.
00:06:41.000 --> 00:06:50.000
This is the same as log, but the second argument specifies the base.
00:06:50.000 --> 00:06:59.000
They also have another function that, if you are doing log10, it's going to default to, and you can actually specify that as a function.
00:06:59.000 --> 00:07:02.000
I think this is made because it is so commonly used.
00:07:02.000 --> 00:07:10.000
So, there is a function called log10; it takes one argument, x, and this calculates the base 10 logarithm of x.
00:07:10.000 --> 00:07:21.000
Here, we can just call math.log10, pass in a value 1, and we get our value 0.0, our base 10 logarithm of x.
00:07:21.000 --> 00:07:30.000
Again, the base 10 logarithm of 10 is 1.0.
00:07:30.000 --> 00:07:35.000
There is also a function to calculate the sine of x.
00:07:35.000 --> 00:07:44.000
Type sin, and pass in the parameter x.
00:07:44.000 --> 00:08:01.000
You can also do square root and tangent: square root is sqrt, and it takes one argument of x; this returns a square root, and the value x must be positive.
00:08:01.000 --> 00:08:11.000
You can also calculate the tangent with tan, and it takes one parameter.
00:08:11.000 --> 00:08:18.000
There are quite a bit of functions; I have gathered a table, a list of them.
00:08:18.000 --> 00:08:26.000
You can go through it and see all the different functions that you can use in this Math module.
00:08:26.000 --> 00:08:39.000
I do have the bang value here; this bang value means that it will update the object it currently is and save it.
00:08:39.000 --> 00:08:49.000
You also have the option to do the method directly, without the bang, which will create a new object with that new calculation.
00:08:49.000 --> 00:08:58.000
There is arc cosine--you add an h; you have the hyperbolic arc cosine.
00:08:58.000 --> 00:09:16.000
We already went through sine: you have arc sine, hyperbolic arc sine; arc tangent--this atan takes an x argument; atan2 takes two arguments, an x and a y argument.
00:09:16.000 --> 00:09:25.000
You have atanh; this is the hyperbolic arc tangent.
00:09:25.000 --> 00:09:29.000
This is the first page; there are a couple more here.
00:09:29.000 --> 00:09:41.000
We already went over cosine; you can use the bang method with your cosine; if you add h, it will get you the hyperbolic cosine.
00:09:41.000 --> 00:09:47.000
If you add sin, that calculates your sine.
00:09:47.000 --> 00:09:54.000
There is an error function in math; do math.erf, and you can get the error function.
00:09:54.000 --> 00:10:01.000
There is also a complementary error function with erfc.
00:10:01.000 --> 00:10:09.000
You can calculate the base x of eular using method exp.
00:10:09.000 --> 00:10:18.000
You can also calculate the normalized fraction exponent using frexp.
00:10:18.000 --> 00:10:22.000
We are almost there; we are on our last page of functions.
00:10:22.000 --> 00:10:41.000
You can calculate the hypotenuse using hypot; you can calculate the floating-point value that corresponds to the mantissa exponent using ldexp.
00:10:41.000 --> 00:11:06.000
You have your sine calculations with sinh--you can calculate the hyperbolic sine; next is square root--sqrt does that--and your tangents--tan and tanh will do your hyperbolic tangent, with the addition of that h value.
00:11:06.000 --> 00:11:10.000
Let's go ahead and look at the Math module.
00:11:10.000 --> 00:11:21.000
If you look at the RDoc, you will notice that it has more examples about all these functions, too.
00:11:21.000 --> 00:11:32.000
This is the Math module; notice that it has the two constants e and π.
00:11:32.000 --> 00:11:43.000
As we just went through that whole list, they do have it on here, too, with the cosine, cosh, sin, sinh...you can see the source for that, too, how it calculates it...
00:11:43.000 --> 00:11:50.000
Notice also, here they tell you the arguments: some take one argument; some take two.
00:11:50.000 --> 00:11:57.000
For the arc tangent, given x, it gives you some examples here.
00:11:57.000 --> 00:12:06.000
You can take a look at that and see how they calculate that.
00:12:06.000 --> 00:12:18.000
Actually, we didn't go over this one: there is a method for cube root: that is cbrt; it takes a parameter of numeric.
00:12:18.000 --> 00:12:23.000
It returns a Float.
00:12:23.000 --> 00:12:30.000
We already went through error: it "calculates the error function of x," and takes x--one parameter.
00:12:30.000 --> 00:12:41.000
exp--e to the power of x--we went over that, and they give you some examples there, also.
00:12:41.000 --> 00:12:49.000
There is a gamma function, and a logarithm gamma function, too.
00:12:49.000 --> 00:12:58.000
As you see here, it's all our log functions that we went over: they overload it, where one has one argument, one has two arguments, one with a base value, and then there is one for log10...
00:12:58.000 --> 00:13:11.000
There is actually one for base 2--log2, so that might be useful to you math calculators.
00:13:11.000 --> 00:13:20.000
That is everything for that Math module.
00:13:20.000 --> 00:13:26.000
That is the Math module; let's move on now to rational numbers.
00:13:26.000 --> 00:13:33.000
Rational numbers: these are numbers that can be expressed as a fraction of integers.
00:13:33.000 --> 00:13:46.000
Just integers; remember that; we are not doing Floats or decimal values; they are all like fractions, essentially; these are all fractions.
00:13:46.000 --> 00:13:55.000
This is supported through the Rational class--remember that Rational class?--it will be useful in this case.
00:13:55.000 --> 00:14:00.000
You must use Integers, not Floats.
00:14:00.000 --> 00:14:06.000
To use this Rational number, you have to load in a couple of libraries.
00:14:06.000 --> 00:14:14.000
The first library you are going to load is this require rational, and then you have to load require mathn.
00:14:14.000 --> 00:14:25.000
We will go deeper into what this mathn library does; it's not required for rational numbers, but it's recommended, so I'm just going to say require that mathn library.
00:14:25.000 --> 00:14:32.000
After you require those two libraries, then you can start using this Rational object.
00:14:32.000 --> 00:14:37.000
For example, here I'm calling rational, and I'm putting my fraction in.
00:14:37.000 --> 00:14:47.000
I put my fraction in: 4 out of 10; after I do that, I press Return, and it's going to automatically reduce down to 2 out of 5.
00:14:47.000 --> 00:14:56.000
I can also do the same thing with 5 out of 10; pass that in, press Return, and it's going to reduce it down to 1 over 2.
00:14:56.000 --> 00:15:01.000
Let's look at some more examples.
00:15:01.000 --> 00:15:22.000
Here, I'm just going to do require.rational, and then I'm going to require mathn, and let's show you the 10 out of 20, and it reduces it down straight to 1 out of 2.
00:15:22.000 --> 00:15:39.000
Rational 3 out of 4 gets me 3 out of 4; rational 1 out of 10--and let's do times 5--so we have 5 out of 10, and it reduces it to 1 out of 2.
00:15:39.000 --> 00:15:46.000
Also, I can go over a whole number, and it does do 3 out of 2.
00:15:46.000 --> 00:15:57.000
You can also do other functions; I can also do exponent, so I can do 1 out of 2 to the power of 2, and it will get me 1 out of 4.
00:15:57.000 --> 00:16:00.000
I can also convert to Strings and Floats.
00:16:00.000 --> 00:16:09.000
If I do Rational (1,4), and convert that to a string, it takes that whole value and puts quotes around it.
00:16:09.000 --> 00:16:24.000
I can do the same thing with Float, and--look there!--it actually gives me the decimal value, .25.
00:16:24.000 --> 00:16:25.000
That is rational numbers.
00:16:25.000 --> 00:16:30.000
Let's look over that mathn library very quickly.
00:16:30.000 --> 00:16:44.000
Mathn is a standard library; it's used for a lot of math functionalities; when used in conjunction with the Math module, it makes mathematical operations better.
00:16:44.000 --> 00:16:51.000
That is just the gist of it; you use it when you are doing math operations; just require it.
00:16:51.000 --> 00:17:02.000
It pulls in other standard libraries, and it integrates them with the rest of Ruby's numeric classes, so it's very useful, so definitely use this library.
00:17:02.000 --> 00:17:10.000
I just gave you a very simple example here, but if I don't include this library and I did 1 out of 2, it will return 0.
00:17:10.000 --> 00:17:15.000
That is not going to work for me; I want specific values.
00:17:15.000 --> 00:17:23.000
If I require this mathn, 1 out of 2 will return 1 out of 2, because it's including these other standard libraries to help do calculations.
00:17:23.000 --> 00:17:33.000
I can show you some examples: let's go ahead and start over and call IRB again--clean slate, nothing is loaded.
00:17:33.000 --> 00:17:39.000
All we do here is, I just pass 1 out of 2, and it returns 0.
00:17:39.000 --> 00:17:51.000
But if I do require.mathn, then I do 1 out of 2, and it gets me the correct answer.
00:17:51.000 --> 00:17:59.000
I can also do calculations with the different functions: 1 out of 2 times 3 over 10--I get 3 over 20.
00:17:59.000 --> 00:18:23.000
I can do a calculation with sqrt, and I get my complex value, with 0 and one imaginary number.
00:18:23.000 --> 00:18:26.000
Next, let's look at complex numbers.
00:18:26.000 --> 00:18:32.000
Complex numbers use the Complex class.
00:18:32.000 --> 00:18:46.000
This is represented as a real number with an imaginary number; I just showed you a terminal view with that...so an example would be 0+1i.
00:18:46.000 --> 00:19:08.000
There are two different types of complex numbers we can use: we can call this polar method, and it will return a complex object denoted in polar form, and I can also call rect, and that will return a complex object denoted in rectangular form.
00:19:08.000 --> 00:19:16.000
You have polar and rectangular.
00:19:16.000 --> 00:19:19.000
Here are some examples of complex numbers.
00:19:19.000 --> 00:19:24.000
You just pass the Complex object, and you pass a parameter in.
00:19:24.000 --> 00:19:31.000
I do Complex(1), and it's going to return me 1+0i.
00:19:31.000 --> 00:19:40.000
You can also pass in a second parameter for imaginary numbers; so, if I call Complex(2,3), it's going to get 2+3i.
00:19:40.000 --> 00:19:47.000
I can pass in these arguments into the rectangular or the polar method.
00:19:47.000 --> 00:19:55.000
If I do Complex.rect and pass (2,3), it's going to get me 2+3i.
00:19:55.000 --> 00:20:01.000
In the polar form, I pass (3,0); it's going to get 3.0+0.0i.
00:20:01.000 --> 00:20:10.000
Here, it passes it in with the decimals; so it has some Float values there.
00:20:10.000 --> 00:20:14.000
Next, let's look at the prime numbers.
00:20:14.000 --> 00:20:18.000
This uses the Prime class.
00:20:18.000 --> 00:20:32.000
It's included in the mathn library, and it starts generating prime numbers, starting from the value 2.
00:20:32.000 --> 00:20:39.000
What is a prime number? It's a value that is divisible by 1 and itself.
00:20:39.000 --> 00:20:41.000
Let's look at some methods.
00:20:41.000 --> 00:20:51.000
One is this method called each: it takes one parameter--if you don't pass anything, it will default that parameter to nil.
00:20:51.000 --> 00:20:58.000
I'm going to call ubound, which is an arbitrary positive number.
00:20:58.000 --> 00:21:02.000
This is known as the upper bound of the enumeration.
00:21:02.000 --> 00:21:15.000
With this method, you can output all the prime numbers to a certain bound, so once it goes over that, it will stop.
00:21:15.000 --> 00:21:19.000
I'll show you an example of that.
00:21:19.000 --> 00:21:27.000
The next one is this value called prime?, and this returns if the value is prime.
00:21:27.000 --> 00:21:48.000
You call itself here, so this would be prime, and then you call the method prime?, and whatever number value you want to check.
00:21:48.000 --> 00:21:52.000
This will return true or false.
00:21:52.000 --> 00:21:58.000
Let's go through an example with the prime values.
00:21:58.000 --> 00:22:04.000
I believe we already have mathn loaded.
00:22:04.000 --> 00:22:12.000
Let's do an example: let's get all the prime numbers up to 10.
00:22:12.000 --> 00:22:40.000
I do each, pass in value 10, and do prime, and then I'm going to output each one, and it starts from the value 2; the next prime number is 3; 5; and 7; it ends at 7--that is where we hit our bound of 10, so it doesn't go over that.
00:22:40.000 --> 00:22:57.000
We can also show you the prime?--let's check: Is 2 prime? It says it is prime; 1 is false; 3 is prime; if I pass the value 4, it says it's false.
00:22:57.000 --> 00:23:07.000
5 and 7 are true; they are prime.
00:23:07.000 --> 00:23:11.000
OK, so those are prime numbers.
00:23:11.000 --> 00:23:15.000
The last thing we want to look at is matrices.
00:23:15.000 --> 00:23:20.000
Matrices are represented through the Matrix class.
00:23:20.000 --> 00:23:28.000
This provides methods for creating matrices, plus manipulating and updating.
00:23:28.000 --> 00:23:33.000
It requires loading the class beforehand.
00:23:33.000 --> 00:23:38.000
For example, I'm going to show you the build method.
00:23:38.000 --> 00:23:47.000
The build method for matrices has two arguments: you need to specify the row size and the column size.
00:23:47.000 --> 00:23:52.000
If you don't specify the column size, it will default to the row size.
00:23:52.000 --> 00:23:57.000
This creates a matrix of row size by column size.
00:23:57.000 --> 00:24:19.000
For example, I call the build method, and I'm doing a row size of 2, and then I'm specifying a column size of 3.
00:24:19.000 --> 00:24:25.000
I'm also passing a block in here; this block says what I'm going to initialize it to.
00:24:25.000 --> 00:24:29.000
In this case, I'm saying to initialize it to 1.
00:24:29.000 --> 00:24:42.000
It makes this new matrix that has values of 1 in it, row size 2, and column--3.
00:24:42.000 --> 00:24:50.000
Let's go through the RDoc.
00:24:50.000 --> 00:24:55.000
This is very basic, but let's go through the RDoc for it.
00:24:55.000 --> 00:25:03.000
Here is the Matrix RDoc; there is quite a bit on methods here, actually.
00:25:03.000 --> 00:25:17.000
"The **Matrix** class represents a mathematical matrix...provides methods for creating matrices, operating on them arithmetically and algebraically, and determining their mathematical properties."
00:25:17.000 --> 00:25:30.000
I showed you the build method, but you will notice that there are quite a bit of methods to build matrices, using different methods: rows, columns, build, diagonal, scalar, identity, zero...
00:25:30.000 --> 00:25:41.000
You can also access the properties of it with quite a bit of functions here: row_size, column_size, row, column, collect, map...
00:25:41.000 --> 00:25:51.000
Properties of a matrix: you can check that, too--you can say, "Hey, is this matrix diagonal? Is it lower_triangular? Regular? Singular? Square?"
00:25:51.000 --> 00:25:56.000
You can check these properties.
00:25:56.000 --> 00:26:07.000
You can also do mathematical arithmetic with it, using multiplication, addition, subtraction, division, inverse, the exponent function...
00:26:07.000 --> 00:26:12.000
You should have quite a bit of functions here to do whatever process you need to do.
00:26:12.000 --> 00:26:21.000
You can use the eigen, the eigensystem, conjugate, imaginary, rectangular, complex arithmetic for the matrices...
00:26:21.000 --> 00:26:28.000
You can also convert it to other types: you can use coerce, to_a, row_vectors, column_vectors.
00:26:28.000 --> 00:26:36.000
You can do string representations with to_s and inspect.
00:26:36.000 --> 00:26:40.000
There are attributes: you have your column_size, your rows...
00:26:40.000 --> 00:26:47.000
If you go through here, you can see a lot of the methods: we went through build, and there are quite a bit of methods here.
00:26:47.000 --> 00:26:52.000
You can create an empty matrix, just with the row_size and the column_size.
00:26:52.000 --> 00:27:03.000
We already went over build, but notice, with build here, I can also pass in what row and column it's working on, so I can initialize values using that.
00:27:03.000 --> 00:27:19.000
I can give it a dynamic value, as they do here: they say, "The value I'm going to pass in is column minus row."
00:27:19.000 --> 00:27:32.000
Then, you have a bunch of instance methods to do matrix calculation.
00:27:32.000 --> 00:27:40.000
You also have this each method as an enumerator, so you can grab elements by what part of the matrix you want.
00:27:40.000 --> 00:27:47.000
You can get all the elements, the ones that are diagonal, strict_lower, strict_upper...
00:27:47.000 --> 00:27:53.000
There are quite a bit of properties there.
00:27:53.000 --> 00:27:59.000
You can also get the Hash code for that matrix...that is interesting.
00:27:59.000 --> 00:28:03.000
You can also get the inverse of the matrix, using the inverse method.
00:28:03.000 --> 00:28:19.000
As I said before, you can check "Is it lower_triangular?", and just say to the matrix, "Hey, are you lower_triangular?"--just call this method, and will say yes, it is, or it isn't--it's false.
00:28:19.000 --> 00:28:25.000
There are quite a bit of functions here, so definitely look at the RDoc.
00:28:25.000 --> 00:28:38.000
That will definitely go over a lot of different methods in the matrix, and help you manipulate, create new matrices, and update existing ones.
00:28:38.000 --> 00:28:44.000
So, definitely go over that, and otherwise, that is the end of this lesson at Educator.com.
00:28:44.000 --> 00:28:51.000
Hope to see you next time, and good luck on your Math module searches and processes!