Sign In | Subscribe

Enter your Sign on user name and password.

Forgot password?
  • Follow us on:
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Math Operations, Part 2

  • In this lesson, we will go over the Math Module, Rational numbers, Complex numbers, Prime numbers, and Matrices
  • The Math module contains methods for basic trignometric and logarithms
  • It also defines constants PI and E
  • Popular used Methods: cos, exp, log, log10, sin, sqrt, tan
  • RDoc:
  • Rational Numbers are supported through the Rational class. They are numbers that can be expressed as a fraction of integers.
  • Mathematical Ruby Scripts (mathn) is used in conjunction with the Math module to make mathematical operations better
  • It pulls in other standard libraries and integrates them with the rest of Ruby's numeric class
  • Complex Numbers represent a paired real number with an imaginary unit
  • Prime Numbers are included in the mathn library. They generate prime numbers starting from 2.
  • Matrices are represented through the Matrix class and provide methods for creating Matrices
  • RDoc:

Math Operations, Part 2

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Math Operations 0:12
    • Math Module
    • Rational Numbers
    • Complex Numbers
    • Prime Numbers
    • Matrices
  • Math Module 1:12
    • PI and E
  • Math Module Methods 2:47
    • atan2(x,y)
    • cos(x)
    • exp(x)
    • Examples
    • log(x)
    • log(num, base)
    • log10(x)
    • sin(x)
    • sqrt(x)
    • tan(x)
  • Math Functions: Part 1 of 3 8:12
  • Math Functions: Part 2 of 3 9:32
  • Math Functions: Part 3 of 3 10:19
    • Math Module Rdoc
  • Rational Numbers 13:23
    • How to use
    • Example
  • Mathematical Ruby Scripts (Mathn) 16:25
    • Example
  • Complex Numbers 18:26
    • polar
    • rect
  • Complex Number Examples 19:18
  • Prime Numbers 20:14
    • each(ubound=nil)
    • prime?
    • Example
  • Matrices 23:15
    • build(row_size, column_size=row_size)
    • Example
    • Matrix Rdoc

Transcription: Math Operations, Part 2

Welcome back to

Today's lesson is on math operations; this is the second part.0002

For today's lesson, we are going to go over five pieces that are part of math operations.0010

The first one we are going to look at is the Math module.0019

This has quite a bit of operations; most of the operations are related to the Math module, so I would give this a star.0023

After that, we are going to look at some of the math types.0038

One is the rational numbers, and after we look at rational numbers, we're going to look at complex numbers.0045

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.0053

First, let's look at the Math module.0072

This is a module that you can include in other objects; you can also use it by itself.0077

It has two constants as part of it: one is pi, and e.0089

To call it, you just call the Math::pi function, and the same with the e: you can just call it with Math::e.0095

The M is uppercase, and the three letters after it are lowercase.0129

That will get those two constant values.0138

The Math module contains a lot of methods for trigonometry and logarithms.0143

It can be invoked through the Math name space or included into scope.0152

Let's look at some of the methods.0164

The first one is atan2; it takes two arguments--in this case, we're giving it an x and a y.0167

This calculates the arc tangent, given the y and the x, which are these two values.0178

The next one is cosine; it uses the method cos; it takes one argument; and it calculates the cosine of x.0188

For our example here, all you do is to call the Math module directly; then, do the period, and call the method cos.0199

In this case, our argument is 0, and that comes up with the cosine 0, and the value is 0.0211

The next method we'll look at is this exponential function.0219

It uses exp, and this is e raised to the power of x, so it is equivalent to ex0224

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.0241

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.0262

Let me show you how to do that.0274

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.0279

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.0292

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.0306

Actually, if that is the case, this is the wrong one; this is 1.0, as we just saw in that terminal.0323

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.0338

As we did in the terminal, math.log(1); you get 0.0.0351

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.0357

What is the natural logarithm of that value?--we get 2.7182, and it goes all the way down.0369

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.0379

You can tell it specifically what base you want it to do.0396

This is the same as log, but the second argument specifies the base.0401

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.0410

I think this is made because it is so commonly used.0419

So, there is a function called log10; it takes one argument, x, and this calculates the base 10 logarithm of x.0422

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.0430

Again, the base 10 logarithm of 10 is 1.0.0441

There is also a function to calculate the sine of x.0450

Type sin, and pass in the parameter x.0455

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.0464

You can also calculate the tangent with tan, and it takes one parameter.0481

There are quite a bit of functions; I have gathered a table, a list of them.0491

You can go through it and see all the different functions that you can use in this Math module.0498

I do have the bang value here; this bang value means that it will update the object it currently is and save it.0506

You also have the option to do the method directly, without the bang, which will create a new object with that new calculation.0519

There is arc cosine--you add an h; you have the hyperbolic arc cosine.0529

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.0538

You have atanh; this is the hyperbolic arc tangent.0556

This is the first page; there are a couple more here.0565

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.0569

If you add sin, that calculates your sine.0581

There is an error function in math; do math.erf, and you can get the error function.0587

There is also a complementary error function with erfc.0594

You can calculate the base x of eular using method exp.0601

You can also calculate the normalized fraction exponent using frexp.0609

We are almost there; we are on our last page of functions.0618

You can calculate the hypotenuse using hypot; you can calculate the floating-point value that corresponds to the mantissa exponent using ldexp.0622

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.0641

Let's go ahead and look at the Math module.0666

If you look at the RDoc, you will notice that it has more examples about all these functions, too.0670

This is the Math module; notice that it has the two constants e and pi.0681

As we just went through that whole list, they do have it on here, too, with the cosine, cosh, sin, can see the source for that, too, how it calculates it...0692

Notice also, here they tell you the arguments: some take one argument; some take two.0703

For the arc tangent, given x, it gives you some examples here.0710

You can take a look at that and see how they calculate that.0717

Actually, we didn't go over this one: there is a method for cube root: that is cbrt; it takes a parameter of numeric.0726

It returns a Float.0738

We already went through error: it "calculates the error function of x," and takes x--one parameter.0743

exp--e to the power of x--we went over that, and they give you some examples there, also.0750

There is a gamma function, and a logarithm gamma function, too.0761

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...0769

There is actually one for base 2--log2, so that might be useful to you math calculators.0778

That is everything for that Math module.0791

That is the Math module; let's move on now to rational numbers.0800

Rational numbers: these are numbers that can be expressed as a fraction of integers.0806

Just integers; remember that; we are not doing Floats or decimal values; they are all like fractions, essentially; these are all fractions.0813

This is supported through the Rational class--remember that Rational class?--it will be useful in this case.0826

You must use Integers, not Floats.0835

To use this Rational number, you have to load in a couple of libraries.0840

The first library you are going to load is this require rational, and then you have to load require mathn.0846

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.0854

After you require those two libraries, then you can start using this Rational object.0865

For example, here I'm calling rational, and I'm putting my fraction in.0872

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.0877

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.0887

Let's look at some more examples.0896

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.0901

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.0922

Also, I can go over a whole number, and it does do 3 out of 2.0939

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.0946

I can also convert to Strings and Floats.0957

If I do Rational (1,4), and convert that to a string, it takes that whole value and puts quotes around it.0960

I can do the same thing with Float, and--look there!--it actually gives me the decimal value, .25.0969

That is rational numbers.0984

Let's look over that mathn library very quickly.0985

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.0990

That is just the gist of it; you use it when you are doing math operations; just require it.1004

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.1011

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.1022

That is not going to work for me; I want specific values.1030

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.1035

I can show you some examples: let's go ahead and start over and call IRB again--clean slate, nothing is loaded.1043

All we do here is, I just pass 1 out of 2, and it returns 0.1053

But if I do require.mathn, then I do 1 out of 2, and it gets me the correct answer.1059

I can also do calculations with the different functions: 1 out of 2 times 3 over 10--I get 3 over 20.1071

I can do a calculation with sqrt, and I get my complex value, with 0 and one imaginary number.1079

Next, let's look at complex numbers.1103

Complex numbers use the Complex class.1106

This is represented as a real number with an imaginary number; I just showed you a terminal view with an example would be 0+1i.1112

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.1126

You have polar and rectangular.1148

Here are some examples of complex numbers.1156

You just pass the Complex object, and you pass a parameter in.1159

I do Complex(1), and it's going to return me 1+0i.1164

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.1171

I can pass in these arguments into the rectangular or the polar method.1180

If I do Complex.rect and pass (2,3), it's going to get me 2+3i.1187

In the polar form, I pass (3,0); it's going to get 3.0+0.0i.1195

Here, it passes it in with the decimals; so it has some Float values there.1201

Next, let's look at the prime numbers.1210

This uses the Prime class.1214

It's included in the mathn library, and it starts generating prime numbers, starting from the value 2.1218

What is a prime number? It's a value that is divisible by 1 and itself.1232

Let's look at some methods.1239

One is this method called each: it takes one parameter--if you don't pass anything, it will default that parameter to nil.1241

I'm going to call ubound, which is an arbitrary positive number.1251

This is known as the upper bound of the enumeration.1258

With this method, you can output all the prime numbers to a certain bound, so once it goes over that, it will stop.1262

I'll show you an example of that.1275

The next one is this value called prime?, and this returns if the value is prime.1279

You call itself here, so this would be prime, and then you call the method prime?, and whatever number value you want to check.1287

This will return true or false.1308

Let's go through an example with the prime values.1312

I believe we already have mathn loaded.1318

Let's do an example: let's get all the prime numbers up to 10.1324

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.1332

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.1360

5 and 7 are true; they are prime.1377

OK, so those are prime numbers.1387

The last thing we want to look at is matrices.1391

Matrices are represented through the Matrix class.1395

This provides methods for creating matrices, plus manipulating and updating.1400

It requires loading the class beforehand.1408

For example, I'm going to show you the build method.1413

The build method for matrices has two arguments: you need to specify the row size and the column size.1418

If you don't specify the column size, it will default to the row size.1427

This creates a matrix of row size by column size.1432

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.1437

I'm also passing a block in here; this block says what I'm going to initialize it to.1459

In this case, I'm saying to initialize it to 1.1465

It makes this new matrix that has values of 1 in it, row size 2, and column--3.1469

Let's go through the RDoc.1482

This is very basic, but let's go through the RDoc for it.1490

Here is the Matrix RDoc; there is quite a bit on methods here, actually.1495

"The Matrix class represents a mathematical matrix...provides methods for creating matrices, operating on them arithmetically and algebraically, and determining their mathematical properties."1503

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...1517

You can also access the properties of it with quite a bit of functions here: row_size, column_size, row, column, collect, map...1530

Properties of a matrix: you can check that, too--you can say, "Hey, is this matrix diagonal? Is it lower_triangular? Regular? Singular? Square?"1541

You can check these properties.1551

You can also do mathematical arithmetic with it, using multiplication, addition, subtraction, division, inverse, the exponent function...1556

You should have quite a bit of functions here to do whatever process you need to do.1567

You can use the eigen, the eigensystem, conjugate, imaginary, rectangular, complex arithmetic for the matrices...1572

You can also convert it to other types: you can use coerce, to_a, row_vectors, column_vectors.1581

You can do string representations with to_s and inspect.1588

There are attributes: you have your column_size, your rows...1596

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.1600

You can create an empty matrix, just with the row_size and the column_size.1607

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.1612

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."1623

Then, you have a bunch of instance methods to do matrix calculation.1639

You also have this each method as an enumerator, so you can grab elements by what part of the matrix you want.1652

You can get all the elements, the ones that are diagonal, strict_lower, strict_upper...1660

There are quite a bit of properties there.1667

You can also get the Hash code for that matrix...that is interesting.1673

You can also get the inverse of the matrix, using the inverse method.1679

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.1683

There are quite a bit of functions here, so definitely look at the RDoc.1699

That will definitely go over a lot of different methods in the matrix, and help you manipulate, create new matrices, and update existing ones.1705

So, definitely go over that, and otherwise, that is the end of this lesson at

Hope to see you next time, and good luck on your Math module searches and processes!1724