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)


#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
# Define some generic vector initialisation and printing operations #
 
COMMENT REQUIRES:
  MODE SCAL = ~ # a scalar, eg REAL #;
  FORMAT scal fmt := ~;
END COMMENT
 
INT vec lwb := 1, vec upb := 0;
MODE VECNEW = [vec lwb:vec upb]SCAL; MODE VEC = REF VECNEW;
FORMAT vec fmt := $"("n(vec upb-vec lwb)(f(scal fmt)", ")f(scal fmt)")"$;
 
PRIO INIT = 1;
 
OP INIT = (VEC self, SCAL scal)VEC: (
  FOR col FROM LWB self TO UPB self DO self[col]:= scal OD;
  self
);
 
# ZEROINIT: defines the additive identity #
OP ZEROINIT = (VEC self)VEC: 
  self INIT SCAL(0);
 
OP REPR = (VEC self)STRING: (
  FILE f; STRING s; associate(f,s);
  vec lwb := LWB self; vec upb := UPB self;
  putf(f, (vec fmt, self)); close(f);
  s
);
 
SKIP