代写代考 juliasheet

juliasheet

This problem sheet is designed to test some basic Julia
knowledge. Note each problem has multiple solutions, and the solution

Copyright By PowCoder代写 加微信 powcoder

sheet will show different ways of solving the same problem.

In assessment, any “solution” will be accepted provided it does the right thing!
So you do not need to be able to write broadcasting or comprehensions
if you can do for loops.

1. Array creation and broadcasting¶
Problem 1.1 Create a vector of length 5 whose entries are Int which is
zero in all entries.

zeros(Int, 5)

5-element Vector{Int64}:

fill(0, 5)

5-element Vector{Int64}:

Comprehension

[0 for k=1:5]

5-element Vector{Int64}:

Problem 1.2 Create a 5×6 matrix whose entries are Int which is
one in all entries.

ret = zeros(Int, 5, 6)
for k=1:5, j=1:6
ret[k,j] = 1

5×6 Matrix{Int64}:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

ones(Int, 5, 6)

5×6 Matrix{Int64}:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

fill(1, 5, 6)

5×6 Matrix{Int64}:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

Comprehension

[1 for k=1:5, j=1:6]

5×6 Matrix{Int64}:
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1

Problem 1.3 Create a 1 × 5 Matrix{Int} with entries A[k,j] = j.

A = zeros(Int, 1, 5)
for j = 1:5
A[1,j] = j

Comprehension

[j for k=1:1, j=1:5]

1×5 Matrix{Int64}:
1 2 3 4 5

convert transpose:

# Note: (1:5)’ is a “row-vector” which behaves differently than a matrix
Matrix((1:5)’)

1×5 Matrix{Int64}:
1 2 3 4 5

Problem 1.4 Create a vector of length 5 whose entries are Float64
approximations of exp(-k). (Two solutions: one using a for loop and
one using broadcasting.)

v = zeros(5) # defaults to Float64
for k = 1:5
v[k] = exp(-k)

Broadcast:

exp.(-(1:5))

5-element Vector{Float64}:
0.36787944117144233
0.1353352832366127
0.049787068367863944
0.01831563888873418
0.006737946999085467

Explicit broadcsat:

broadcast(k -> exp(-k), 1:5)

5-element Vector{Float64}:
0.36787944117144233
0.1353352832366127
0.049787068367863944
0.01831563888873418
0.006737946999085467

Comprehension:

[exp(-k) for k=1:5]

5-element Vector{Float64}:
0.36787944117144233
0.1353352832366127
0.049787068367863944
0.01831563888873418
0.006737946999085467

Problem 1.5 Create a 5 × 6 matrix A whose entries A[k,j] == cos(k+j).

A = zeros(5,6)
for k = 1:5, j = 1:6
A[k,j] = cos(k+j)

5×6 Matrix{Float64}:
-0.416147 -0.989992 -0.653644 0.283662 0.96017 0.753902
-0.989992 -0.653644 0.283662 0.96017 0.753902 -0.1455
-0.653644 0.283662 0.96017 0.753902 -0.1455 -0.91113
0.283662 0.96017 0.753902 -0.1455 -0.91113 -0.839072
0.96017 0.753902 -0.1455 -0.91113 -0.839072 0.0044257

Broadcasting:

cos.(k .+ j’)

5×6 Matrix{Float64}:
-0.416147 -0.989992 -0.653644 0.283662 0.96017 0.753902
-0.989992 -0.653644 0.283662 0.96017 0.753902 -0.1455
-0.653644 0.283662 0.96017 0.753902 -0.1455 -0.91113
0.283662 0.96017 0.753902 -0.1455 -0.91113 -0.839072
0.96017 0.753902 -0.1455 -0.91113 -0.839072 0.0044257

Broadcasting (explicit):

broadcast((k,j) -> cos(k+j), 1:5, (1:6)’)

5×6 Matrix{Float64}:
-0.416147 -0.989992 -0.653644 0.283662 0.96017 0.753902
-0.989992 -0.653644 0.283662 0.96017 0.753902 -0.1455
-0.653644 0.283662 0.96017 0.753902 -0.1455 -0.91113
0.283662 0.96017 0.753902 -0.1455 -0.91113 -0.839072
0.96017 0.753902 -0.1455 -0.91113 -0.839072 0.0044257

Problem 2.1 Create a type Rat with two Int fields, p and q,
representing a rational function including +, *, -, and /.
Bonus points: Use gcd and div to reduce.

# import allows us to overload basic operations
import Base: +, *, -, /

# struct creates a new type called `Rational`
struct Rat
# Here the two fields are both Int

# overload + for adding two rationals.
function +(a::Rat, b::Rat)
p = a.p*b.q + b.p*a.q
q = a.q*b.q
d = gcd(p,q)
Rat(p ÷ d, q ÷ d)

-(a::Rat) = Rat(-a.p, a.q)
-(a::Rat, b::Rat) = a + (-b)

function *(a::Rat, b::Rat)
p = a.p * b.p
q = a.q * b.q
d = gcd(p,q)
Rat(p ÷ d, q ÷ d)

/(a::Rat, b::Rat) = a * Rat(b.q, b.p)

@test Rat(1,2) + Rat(2,3) == Rat(7,6)
@test Rat(1,2) * Rat(2,3) == Rat(1,3)
@test Rat(1,2) / Rat(2,3) == Rat(3,4)

Test Passed
Expression: Rat(1, 2) / Rat(2, 3) == Rat(3, 4)
Evaluated: Rat(3, 4) == Rat(3, 4)

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com