Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

This repository contains the course materials from Math 157: Intro to Mathematical Software.

Creative Commons BY-SA 4.0 license.

Views: 3037
License: OTHER
Kernel: SageMath 8.1

Math 157: Intro to Mathematical Software

UC San Diego, winter 2018

March 5, 2018: Julia (part 2 of 3)

Guest lecture by Alyson Deines (Center for Communications Research)

Administrivia:

  • CAPE evaluations are available! They close Monday, March 19 at 8am. Since this course is highly experimental, your feedback will be very helpful for shaping future offerings.

  • No sections on Tuesday, March 6. However, during this time, you are welcome to use APM 6402 as a study room; we will also try to monitor the chat room.

  • Thomas's office hours (usually Tuesday 11am-12pm) are moved to Friday 11:30am-12:50pm.

  • Peter's office hours (usually Wednesday 3-5pm) are moved to Wednesday 5-7pm.

  • There will be an extra virtual office hour Thursday 6-7pm.

Advance notice for week 10:

  • No lectures on Monday, March 12 or Wednesday, March 14. You may wish to use this time to meet your final project group.

  • There will be a lecture on Friday, March 16, on the topic of "Where to go from here?" This lecture will not be counted for course attendance; that is, the last lecture for which attendance counts is Friday, March 9.

  • My office hours on Thursday, March 15 are cancelled. All other sections and office hours meet as scheduled.

Final project news

  • Both parts will be collected Sunday, March 18 at 8pm.

  • The groups for part 2 have been assigned. See the file final_project_groups.md in the shared project; then contact your group members as soon as possible. (I have created workspaces and chat rooms which may help with this; these are described in the same file.)

  • For part 1, problem 4b, instead of the exhaustive search, do random sampling with 10410^4 samples. Ditto for 4c.

Vectors and Matrices

Julia views vectors as flat arrays and matrices as two-dimentional arrays.

To get a "row" vector:

[1 2 3]
1×3 Array{Int64,2}: 1 2 3

To get a "column" vector:

[1, 2, 3]
3-element Array{Int64,1}: 1 2 3
v = [2, -1, 4] w = [1, 0, -3] println(v) println(w)
[2, -1, 4] [1, 0, -3]
v + 3*w # Addition, scalar multiplication
dot(v, w)
cross(v, w)
v*transpose(w)
transpose(v)*w
# Easier transpose v'*w
# Doesn't automatically dot v*w
?norm
norm(v, 1)
norm(v, 2)
sqrt(v'v)

Vectorized Functions

A vectorized function is a function f(x)f(x) that simply applies to every element of an array AA to yield f(A)f(A). The syntax in Julia for this is f.(A)f.(A).

log(1.0)
log.(ones(4))
v
log.(complex.(v'))
sin.(v')

Matrices:

M = [1 -1 0; -2 2 -1; 0 1 3] det(M)
trace(M)
eigvals(M)
evals, evecs = eig(A)
rank(M)
nullspace(M)
typeof(M)
# Careful M0 = [[1, -1, 0], [-2, 2, -1], [0, 1, 3]] println(typeof(M0)) M1 = [[1 -1 0] [-2 2 -1] [0 1 3]] println(typeof(M1))
M1 == M
M
M0
M1
M == [[1 -1 0]; [-2 2 -1]; [0 1 3]]
# And keep being careful [v w]
[transpose(v); transpose(w)]
[v'; w']
I3 = eye(Int, 3) # Identity matrix
M + I3 #Matrix addition
# Note, assumes float not int: eye(3)
M*M # Matrix multiplication

You don't always need * for multiplication

M'M

Here is where things start looking even more like numpy and a lot less like Sage.

# Rows, Cols size(M)
# Careful M[1] # Doesn't extract a row
# Extract a row M[1, :]'
# Extract a column M[:, 2]
M
# Stacks columns as a vector M[:]
# Stacks rows as a vector M'[:]

Solve a system. Note we can force the matrix and vector to have entries of a specific type.

A = Rational[3 2 -1; 2 1 -1; 1 -1 1] b = Rational[1, -1, 2] A\b

vs.

A = [3 2 -1; 2 1 -1; 1 -1 1] b = [1, -1, 2] A\b
w = [1/3, 5/3, 10/3]
A*w
inv(A)
A

Several ways to add rows and columns:

[A;[1 3 4]]
[A; [1, 3, 4]']
cat(1, A, [1 3 4])
[A';[1, 3, 4]']'
cat(2, A, [1, 3, 4])
[A M]
[A; M]
zeros(5, 10)
diag(A)
diag(M)

A few numpy-ish functions on matrices

A^3
sum(A)
mean(A)
max.(A, M)
min.(A, M)
norm(A)
norm(A[:])
?norm