0% found this document useful (0 votes)
38 views4 pages

Julia Exp7

Julia programming language - 4th sem Lab experiment 7

Uploaded by

sahana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Julia Exp7

Julia programming language - 4th sem Lab experiment 7

Uploaded by

sahana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JULIA PROGRAMMING

7.a. Develop a Julia program to determine the following properties of a matrix:


determinant, inverse, rank, upper & lower triangular matrix, diagonal elements,
Euclidean norm and Square Root of a Matrix.

Theory:

Code:

using LinearAlgebra

function main()
# Example matrix
A = [1.0 2.0 3.0;
4.0 5.0 6.0;
7.0 8.0 10.0]
println(A)
println()

# Determinant
det_A = det(A)
println("Determinant of A:")
println(det_A)
println()

# Inverse
inv_A = inv(A)
println("Inverse of A:")
println(inv_A)
println()

# Rank
rank_A = rank(A)
println("Rank of A:")
println(rank_A)
println()

# Upper triangular matrix


upper_A = UpperTriangular(A)
println("Upper triangular matrix of A:")
println(upper_A)

SAHANA V, DEPT. OF AIML, GEC, CHALLAKERE 58


JULIA PROGRAMMING

println()

# Lower triangular matrix


lower_A = LowerTriangular(A)
println("Lower triangular matrix of A:")
println(lower_A)
println()

# Diagonal elements
diag_A = diag(A)
println("Diagonal elements of A:")
println(diag_A)
println()

# Euclidean norm
norm_A = norm(A)
println("Euclidean norm of A:")
println(norm_A)
println()

# Square root of matrix A (matrix B such that B*B = A)


sqrt_A = sqrt(A)
println("Square root of A:")
println(sqrt_A)
println()
end

# Call the main function to execute the program


main()

SAHANA V, DEPT. OF AIML, GEC, CHALLAKERE 59


JULIA PROGRAMMING

7.b.Develop a Julia program to determine addition and subtraction of two


matrices (element -wise)

Theory:

Code:

# Function to add two matrices


function matrix_addition(A, B)
# Check if matrices have the same dimensions
if size(A) != size(B)
error("Matrices must have the same dimensions for addition.")
end

m, n = size(A)
C = zeros(m, n) # Initialize a matrix to store the result

for i in 1:m
for j in 1:n
C[i, j] = A[i, j] + B[i, j]
end
end

return C
end

# Function to subtract two matrices


function matrix_subtraction(A, B)
# Check if matrices have the same dimensions
if size(A) != size(B)
error("Matrices must have the same dimensions for subtraction.")
end

m, n = size(A)
D = zeros(m, n) # Initialize a matrix to store the result

for i in 1:m
for j in 1:n

SAHANA V, DEPT. OF AIML, GEC, CHALLAKERE 60


JULIA PROGRAMMING

D[i, j] = A[i, j] - B[i, j]


end
end

return D
end

# Example usage
# Define two matrices A and B
A = [1 2 3;
4 5 6;
7 8 9]

B = [9 8 7;
6 5 4;
3 2 1]

# Perform addition
C = matrix_addition(A, B)
println("Result of addition:")
println(C)

# Perform subtraction
D = matrix_subtraction(A, B)
println("\nResult of subtraction:")
println(D)

SAHANA V, DEPT. OF AIML, GEC, CHALLAKERE 61

You might also like