main.jl (1860B)
1 #/usr/bin/julia 2 3 include("./functions.jl") 4 5 using LinearAlgebra 6 7 function main() 8 # Exercise 3 9 k = 2 10 r = 1 11 n = (4, 3) 12 13 S = rand(1:10, n) # n_1 x ... x n_d array 14 Z = rand(1:10, r, n[k]) # r x n_k array for k in {1,...,d}, r natural number 15 T = mode_k_dot(S, Z, k) # n_1 x ... x n_{k-1} x r x n_{k+1} x ... x n_d 16 println("\n\nExercise 3") 17 println("\nMode-k contraction of $(size(S)) with $(size(Z)) gets $(size(T))") 18 19 # Exercise 4 20 U_1 = rand(1:10, 5, 3) 21 U_2 = rand(1:10, 4, 3) 22 U_3 = rand(1:10, 3, 3) 23 U_4 = rand(1:10, 2, 3) 24 U_5 = rand(1:10, 1, 3) 25 U = [U_1, U_2, U_3, U_4, U_5] 26 27 s = cpd_eval(U) 28 println("\n\nExercise 4") 29 println("CPD evaluation of U_d (d=5) of sizes $(size(U_1)) ... $(size(U_5)): $(size(s))") 30 31 # Exercise 5 32 println("\n\nExercise 5") 33 for n=1:8 34 T, (U, V, W)= multiplication_tensor(n) 35 println("For n=$n T == cpd_eval(U, V, W): ", cpd_eval([U, V, W]) == T) 36 end 37 38 # Exercise 6 39 n = 4 # can take bigger n 40 A = rand(1:10, n, n) 41 B = rand(1:10, n, n) 42 T, (U, V, W) = multiplication_tensor(n) 43 println("\n\nExercise 6") 44 println("\n Multiplication Tensor order $n") 45 display(T) 46 println("\nCPD multiplication yields: $(A*B == cpd_multiply(A, B, U, V, W))") 47 48 # Exercise 7 49 U, V, W = rank_7_CPD() 50 A = rand(1:10, 2, 2) 51 B = rand(1:10, 2, 2) 52 println("\n\nExercise 5") 53 println("\nTiming Strassens A⋅B") 54 println(@time strassen_alg(A, B)) 55 56 println("\nTiming julia's A⋅B") 57 println(@time A*B) 58 println("\nDoes Strassen Algorithm get the right results? :$(strassen_alg(A,B) == A*B)") 59 60 println("\nrank 7 CPD T yields the right tensor $(cpd_eval([U, V, W]) == multiplication_tensor(2)[1])") 61 println("\nCPD multiplication yields: $(A*B == cpd_multiply(A, B, U, V, W))") 62 end 63 64 main()