Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download

Math 208 Interactive Notebooks © 2024 by Soham Bhosale, Sara Billey, Herman Chau, Zihan Chen, Isaac Hartin Pasco, Jennifer Huang, Snigdha Mahankali, Clare Minerath, and Anna Willis is licensed under CC BY-ND 4.0

Views: 849
License: OTHER
Image: ubuntu2204
Kernel: SageMath 10.2
# L.A. with sage: Chapter 1

Isaac Hartin Pasco

Preamble: How to very basically use this. Cells are things you can type in and can be run. To run a cell you click on it with the left mouse button and then press shift and enter at the same time. In between Cells you should have a tooltip that appears if you hover your mouse there that says "Code Text Paste Generate". Code lets you type sage/python code and you run shift and enter to run it. Text lets you form a text box like this one that you can type LaTeX\LaTeX and Markdown in pressing shift and enter in a text box formats it so it looks nice. Don't worry about the paste or generate boxes I don't know what they do. If you want to undo something control+z appears to do that. if you want to delete a cell go to edit in the top left next to file and go to delete cell. If you want to edit a cell you can double click it with the left mouse button.

A note about variables in python specifically, NOT ABOUT VARIABLES IN THE EQUATION STUFF. A variable in python is a box that contains something. you tell python what kind of something you want you want to have a variable to hold using "Variable"="something""Variable" = "something". but without the quotes. You can set a variable to be the result of a mathematical expression for example x=2x = 2 or x=22x = 2*2 you use ++ to add - to subtract // to divide * to multiply and ^ to take something to a power. Note that this works with things that aren't numbers and depending on what they are, for example x="hi"+" there"x = "hi"+"\ there" would make x be "hi there". you can also declare variables in terms of other variables and mathematical expressions with other variables assuming those variables already exist. Mathematical expressions obey order of operations and you can use parenthesis in them. One final thing. Code cells are evaluated from top to bottom and its a bad idea to do more than one equals sign per line. (the ==== signifies two things actually being equal and you can have multiple of those just fine).

Alright. Finally we can get to linear equations. A linear equation is an equation that can be represented in the form s0x0+s1x1+s2x2+...+snxn=ys0​x0​+s1​x1​+s2​x2​+...+sn​xn​=y where each of x0...xnx0​...xn​ is a variable and each of s0...sns0​...sn​ and yy is a scalar. these are very cool and useful for a lot of reasons that can be gotten into later. sage math and computer things make it so you can manipulate and solve them without having to brute force things and allows for generalizations and optimizations in the process of doing algebra that can be very useful. The sage documentation on linear algebra https://doc.sagemath.org/html/en/tutorial/tour_linalg.html is an excellent resource on this sort of thing.

#you can declare variables you can use in equation like this x0,x1 = var("x0,x1"); #or like this x = var("x1") #you can declare variables you just want to use but not in a equation specifically like this x = 2 y = "Linear equations are fun :3" #if you feel like it you can also set variables equal to each other x = y print(x) #note that the assignment here is only to x, y won't be set equal to x by this print(y) #note the second, if you want to print something out specifically you can use print(that thing) to print it print(2) #if you want to print multiple things at once you can use a comma between them in the printing to print both of then out x = "like this!" print("see?", x)
Linear equations are fun :3 Linear equations are fun :3 2 see? like this!

note that the variables can be named almost anything they don't have to be named xx or xSomethingx Something. Just make sure they are letters and numbers and don't start with numbers e.g n3n3 is a valid variable name and nop3snop3s is a valid variable name but 3n3n is not and neither is 33. They have to be ascii i think.

#you can declare an equation like this x0,x1 = var("x0,x1"); f = [x0+x1 == 6]

The equation can be written in normal equation writing like you would type it up, but you use ** instead of power and a ==== instead of the normal equals signs because those are used to assign stuff to variables. also it doesn't multiply automatically if you put two things together if you want to multiply something you must put * between the two things or else it won't work. The equations also obey PEMDAS if you were curious

#you can declare two equations like this x0,x1 = var("x0,x1"); f = [x0+x1 == 6] g = [2*x0+3*x1 == 8]

Equations and variables use the same naming conventions, anything that can be a valid variable name can be a valid equation name

#if you want to see what an equation looks like you can use x = var("x") plot(6-x,(x, -10,10)) #6-x here is turning x0+x1 = 6 into x0 = 6-x1 and graphing that as y = 6-x.
Image in a Jupyter notebook
#if you want to plot something in 3d you can use x0,x1 = var("x0,x1") plot3d(2*x0+3*x1, (x0, -10, 10), (x1,-10,10))
Graphics3d Object

For plotting the first value is an expression that either produces the y value on the graph or the z value of the graph depending on whether it is 3d, and the second and potential third parts tell them what values to evaluate the variables under. For example (x,10,10)(x,-10,10) tells python to evaluate the value of x from -10 to 10

#you can declare a system of equations like this x0,x1 = var("x0,x1"); f = [x0+x1 == 6] g = [2*x0+3*x1 == 8] s = [f,g]

Systems of equations obey the same naming conventions as everything else. Please do not name things the same name as other things it will make your life very hard.

#you can solve a system of equations like this x0,x1 = var("x0,x1") f = [x0+x1 == 0] g = [2*x0+3*x1 == 0] s = [f,g] solve(s, x0,x1) #or like this solve([f,g],x0,x1) #or like this solve([[x0+x1 == 0], 2*x0+3*x1 == 0],x0,x1) # solve(s,x0,x1) does exactly the same thing as solve([f,g],x0,x1) and solve([[x0+x1 == 6], 2*x0+3*x1 == 8],x0,x1) does
[[x0 == 0, x1 == 0]]

you can represent a system of linear equations by putting the coefficients of the linear equations into a matrix. this makes it so you can do things to that system of equations without having to worry about the original equations and allow you to treat that system as an object you can do math on,

#for example you could represent the system of linear equations used above, x0+x1 = 0 and 2x0+3x1 == 0 as A = matrix([[1,1], [2,3]]) #you can get the transpose of a matrix just by using A.transpose() #and you can print a matrix the same as anything else by using print(A)
[1 1] [2 3]

although the above is a perfectly valid way of creating a matrix for reasons of convention the rest of this will use a different method of writing the columns of a matrix and then using .transpose to get the matrix right because that is a bit easier to work with

#the conventional way of doing matrices A = matrix([[1,2], [1,3]]).transpose() print("A: ") print(A) #this is convenient because it lets you declare and manipulate collumn vectors seperately w = [1,2] x = [1,3] B = matrix([w,x]).transpose() print("B: ") print(B)
A: [1 1] [2 3] B: [1 1] [2 3]

There are various operations you can do on matrices, but the fundamental ones you might want to use in solving a linear equation are swapping the postions of two rows, multiply a row by something that isn't zero and add a nonzero multiple of a row to another row

A = matrix([[1,1], [2,3]]).transpose() print("before anything A is") print(A) # to add one row to another use A.add_multiple_of_row(0,1,1) # adds 1 times the second row to the first row, #in order the things 0,1,1 do are, the zeroth row is the row to be added to, the first row is the row to add, multiply the row to add by 1 #note that the rows are indexed such that the the first row is called 0, the second row is called 1 etc print("after adding A is") print(A) #to multiply a row by another use A.rescale_row(0,2) print("after scaling the first row by 2 A is: ") print(A) #to swap two rows use A.swap_rows(0,1) #order doesn't really matter here but swaps the first and the second row print("After swapping the first and second rows A is: ") print(A)
before anything A is [1 2] [1 3] after adding A is [2 5] [1 3] after scaling the first row by 2 A is: [ 4 10] [ 1 3] After swapping the first and second rows A is: [ 1 3] [ 4 10]

Matrices can also be added and subtracted etc but that is more complicated so don't worry about it for now :):)

You do need to know about python stuff though so one last thing. Python Functions are a way of doing a bunch of extremely cool things. Don't put them into the linear equations we talked about above because they sort of break them but otherwise they are very useful. they work like functions in math in that you can use them with some arguments and depending on the function you get something back out. they can also work as subroutines where your code goes off and does something for a second, and then goes back to what it was doing before. they also might be both.

def f(x): # you define a function using def, the function name and in parenthesis the variables it takes in. functions in python are determined by indentation #so the continous stuff that is at this level or more of indentation is part of this function #you put any code you want to run in the function here print(x) # for example return str(x) # the value this function returns, if you don't put a return the function returns nothin #this is where the function ends y = f(2) #this calls the function with a value of 2, while inside the function anything called x will actually contain 2, y is the value that is returned
2
'2'

Note that unlike other things in a python file functions are not run unless called and if they are called they are run at the point where they are called. they are also effectively replaced with whatever they return so y = f(2) is equivalent to y = "2" in the above case although nothing would be printed out from f() if it was just y = "2"

#functions can be called with nearly anything as long as the operations inside the function are things that can be done to the things passed into it, numbers and strings of characters won't be changed by things done to them in a function other things MAY def f(x): return str(x)+" f was called on this" y = f("howdy") print(y) z = f(y) print(z) print(y)# note that y stays the same after f is called on it
howdy f was called on this howdy f was called on this f was called on this howdy f was called on this
#you can also have a function that returns nothing def f(x): x = x+1 f(2) #note that this runs even though it doesn't print anything

TrueTrue and FalseFalse are also valid things for a variable to be and you can do things with them or get values of TrueTrue or FalseFalse from other expressions. For example 0==10 == 1 evaluates to FalseFalse and 1==11 == 1 evaluates to TrueTrue. putting !! in front of something changes it to the opposite of whatever it was if it's a boolean value. !True!True is FalseFalse for example. You can concatenate boolean(things that are either TrueTrue or FalseFalse) expressions together to get more complicated logic

# x==y is true if x is equal to y or false otherwise a = 0 == 1 print(a) b = 1 == 1 print(b) # x<y is true if x<y or false otherwise c = 1<2 print(c) # x>y is true if x>y or false otherwise d = 1>2 print(d) # x<= y is true if x<=y or false otherwise e = 1<=2 print(e) # x>= y is true if x>=y or false otherwsie f = 1>=2 print(f)
False True True False True False
#x & y is true if both x and y are true or false otherwise a = (1<2) &(2<3) print(a) #x | y is true if one of x or y are true false if both of them are false b = (1>2) | (2<3) print(b)
True True

If you want something to only run if some particular thing is true you can use if to do that. if you follow and if with an "elif" if the first if expression sn't true the "elif" will evalute if the second expression is true. an else will execute if the if preceding it doesn't execute. Note you can chain elifs.

x = 1 if x == 1: print("x was 1 ") elif x == 2: print("x was 2 ") else: print("x was neither 1 nor 2") #note that the rules for whether or not something is inside of an if statement are the statement are the same as the rules for if something is inside a function. #try changing what x is here and seeing what happens #second note much like functions you can run anything inside an if statement. this includes other if statements.
x was 1
#example of nested if statements. THIS IS BAD CODE DO NOT USE IT FOR ANYTHING def is_even(x): if x == 2: return True else: if x == 4: return True else: if x == 6: return True else: if x == 8: return True else: if x == 10: return True else: return -1 print(is_even(2))