编程代写 Untitled16

Untitled16

1. Pitfalls in Julia¶

Copyright By PowCoder代写 加微信 powcoder

# StackOverflow

function f(x)

f (generic function with 1 method)

StackOverflowError:

Stacktrace:
[1] f(x::Int64) (repeats 79984 times)
@ Main ./In[1]:4

# MyMatrix(A) will have same entries as A, defined by A*x

struct MyMatrix <: AbstractMatrix{Float64} A::Matrix{Float64} import Base: size, getindex, * size(A::MyMatrix) = size(A.A) *(A::MyMatrix, x::AbstractVector) = A.A * x function getindex(A::MyMatrix, k::Int, j::Int) n = size(A,1) eⱼ = zeros(n) # Now it is a Vector (A * eⱼ)[k] # eₖ' * A * eⱼ == A[k,j] getindex (generic function with 220 methods) A = MyMatrix(randn(3,3)) # error is because printing calls size 3×3 MyMatrix: -0.646254 -0.150629 0.51607 0.850809 0.183315 -2.40301 -1.54714 -0.908848 -2.0737 3×3 Matrix{Float64}: -0.646254 -0.150629 0.51607 0.850809 0.183315 -2.40301 -1.54714 -0.908848 -2.0737 2. Using decompositions¶ 1. LU (no pivoting)¶ using LinearAlgebra A = randn(n,n) L, U = lu(A, NoPivot()) norm(L*U - A) 4.598248683588176e-10 T = Tridiagonal(randn(n-1), randn(n), randn(n-1)) L,U = lu(T, NoPivot()) # L and U (should be) Bidiagonal L,U,p = lu(A) P = I(n)[p,:] norm(P'*L*U - A) 4.7814256932425605e-12 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com