Task

Build an   identity matrix   of a size known at run-time.



An identity matrix is a square matrix of size n × n,

where the diagonal elements are all 1s (ones),

and all the other elements are all 0s (zeroes).







I

n


=


[



1


0


0


⋯


0




0


1


0


⋯


0




0


0


1


⋯


0




⋮


⋮


⋮


⋱


⋮




0


0


0


⋯


1



]




{\displaystyle I_{n}={\begin{bmatrix}1&0&0&\cdots &0\\0&1&0&\cdots &0\\0&0&1&\cdots &0\\\vdots &\vdots &\vdots &\ddots &\vdots \\0&0&0&\cdots &1\\\end{bmatrix}}}





Related tasks

   Spiral matrix
   Zig-zag matrix 
   Ulam_spiral_(for_primes)


 
class
	APPLICATION
 
inherit
	ARGUMENTS
 
create
	make
 
feature {NONE} -- Initialization
 
	make
			-- Run application.
		local
		    dim : INTEGER -- Dimension of the identity matrix
		do
		    from dim := 1 until dim > 10 loop
		    	print_matrix( identity_matrix(dim) )
				dim := dim + 1
				io.new_line
			end
 
		end
 
feature -- Access
 
	identity_matrix(dim : INTEGER) : ARRAY2[REAL_64]
 
		require
			dim > 0
		local
			matrix : ARRAY2[REAL_64]
			i : INTEGER
		do
 
			create matrix.make_filled (0.0, dim, dim)
			from i := 1 until i > dim loop
				matrix.put(1.0, i, i)
				i := i + 1
			end
 
			Result := matrix
		end
 
	print_matrix(matrix : ARRAY2[REAL_64])
		local
			i, j : INTEGER
		do
			from i := 1 until i > matrix.height loop
				print("[ ")
				from j := 1 until j > matrix.width loop
					print(matrix.item (i, j))
					print(" ")
					j := j + 1
				end
				print("]%N")
				i := i + 1
			end
		end
 
end
 