This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,33 @@
#!/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

View file

@ -0,0 +1,52 @@
# -*- coding: utf-8 -*- #
# Define some generic matrix initialisation and printing operations #
COMMENT REQUIRES:
MODE SCAL = ~ # a scalar, eg REAL #;
MODE VEC = []SCAL;
FORMAT scal fmt := ~;
et al.
END COMMENT
INT mat lwb := 1, mat upb := 0;
MODE MATNEW = [mat lwb:mat upb, vec lwb: vec upb]SCAL; MODE MAT = REF MATNEW;
FORMAT mat fmt = $"("n(vec upb-vec lwb)(f(vec fmt)","lx)f(vec fmt)")"l$;
PRIO DIAGINIT = 1;
OP INIT = (MAT self, SCAL scal)MAT: (
FOR row FROM LWB self TO UPB self DO self[row,] INIT scal OD;
self
);
# ZEROINIT: defines the additive identity #
OP ZEROINIT = (MAT self)MAT:
self INIT SCAL(0);
OP REPR = (MATNEW self)STRING: (
FILE f; STRING s; associate(f,s);
vec lwb := 2 LWB self; vec upb := 2 UPB self;
mat lwb := LWB self; mat upb := UPB self;
putf(f, (mat fmt, self)); close(f);
s
);
OP DIAGINIT = (MAT self, VEC diag)MAT: (
ZEROINIT self;
FOR d FROM LWB diag TO UPB diag DO self[d,d]:= diag[d] OD;
# or alternatively using TORRIX ...
DIAG self := diag;
#
self
);
# ONEINIT: defines the multiplicative identity #
OP ONEINIT = (MAT self)MAT: (
ZEROINIT self DIAGINIT (LOC[LWB self:UPB self]SCAL INIT SCAL(1))
# or alternatively using TORRIX ...
(DIAG out) VECINIT SCAL(1)
#
);
SKIP

View file

@ -0,0 +1,11 @@
# -*- coding: utf-8 -*- #
PRIO IDENT = 9; # The same as I for COMPLex #
OP IDENT = (INT lwb, upb)MATNEW:
ONEINIT LOC [lwb:upb,lwb:upb]SCAL;
OP IDENT = (INT upb)MATNEW: # default lwb is 1 #
1 IDENT upb;
SKIP

View file

@ -0,0 +1,8 @@
#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
PR READ "prelude/vector_base.a68" PR;
PR READ "prelude/matrix_base.a68" PR;
PR READ "prelude/matrix_ident.a68" PR;
SKIP

View file

@ -0,0 +1,9 @@
#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
MODE SCAL = REAL;
FORMAT scal fmt := $g(-3,1)$;
PR READ "prelude/matrix.a68" PR;
print(REPR IDENT 4)

View file

@ -0,0 +1,47 @@
template<class T>
class matrix
{
public:
matrix( unsigned int nSize ) :
m_oData(nSize * nSize, 0), m_nSize(nSize) {}
inline T& operator()(unsigned int x, unsigned int y)
{
return m_oData[x+m_nSize*y];
}
void identity()
{
int nCount = 0;
int nStride = m_nSize + 1;
std::generate( m_oData.begin(), m_oData.end(),
[&]() { return !(nCount++%nStride); } );
}
inline unsigned int size() { return m_nSize; }
private:
std::vector<T> m_oData;
unsigned int m_nSize;
};
int main()
{
int nSize;
std::cout << "Enter matrix size (N): ";
std::cin >> nSize;
matrix<int> oMatrix( nSize );
oMatrix.identity();
for ( unsigned int y = 0; y < oMatrix.size(); y++ )
{
for ( unsigned int x = 0; x < oMatrix.size(); x++ )
{
std::cout << oMatrix(x,y) << " ";
}
std::cout << std::endl;
}
return 0;
}

View file

@ -0,0 +1,23 @@
#include <boost/numeric/ublas/matrix.hpp>
int main()
{
using namespace boost::numeric::ublas;
int nSize;
std::cout << "Enter matrix size (N): ";
std::cin >> nSize;
identity_matrix<int> oMatrix( nSize );
for ( unsigned int y = 0; y < oMatrix.size2(); y++ )
{
for ( unsigned int x = 0; x < oMatrix.size1(); x++ )
{
std::cout << oMatrix(x,y) << " ";
}
std::cout << std::endl;
}
return 0;
}

View file

@ -0,0 +1,35 @@
MODULE Algebras;
IMPORT StdLog,Strings;
TYPE
Matrix = POINTER TO ARRAY OF ARRAY OF INTEGER;
PROCEDURE NewIdentityMatrix(n: INTEGER): Matrix;
VAR
m: Matrix;
i: INTEGER;
BEGIN
NEW(m,n,n);
FOR i := 0 TO n - 1 DO
m[i,i] := 1;
END;
RETURN m;
END NewIdentityMatrix;
PROCEDURE Show(m: Matrix);
VAR
i,j: INTEGER;
BEGIN
FOR i := 0 TO LEN(m,0) - 1 DO
FOR j := 0 TO LEN(m,1) - 1 DO
StdLog.Int(m[i,j]);
END;
StdLog.Ln
END
END Show;
PROCEDURE Do*;
BEGIN
Show(NewIdentityMatrix(5));
END Do;
END Algebras.

View file

@ -0,0 +1,6 @@
link matrix
procedure main(argv)
if not (integer(argv[1]) > 0) then stop("Argument must be a positive integer.")
matrix1 := identity_matrix(argv[1], argv[1])
write_matrix(&output,matrix1)
end

View file

@ -0,0 +1 @@
I = eye(10)

View file

@ -0,0 +1,19 @@
/* NetRexx ************************************************************
* show identity matrix of size n
* I consider m[i,j] to represent the matrix
* 09.07.2013 Walter Pachl (translated from REXX Version 2)
**********************************************************************/
options replace format comments java crossref symbols binary
Parse Arg n .
If n='' then n=5
Say 'Identity Matrix of size' n '(m[i,j] IS the Matrix)'
m=int[n,n] -- Allocate 2D square array at run-time
Loop i=0 To n-1 -- Like Java, arrays in NetRexx start at 0
ol=''
Loop j=0 To n-1
m[i,j]=(i=j)
ol=ol m[i,j]
End
Say ol
End

View file

@ -0,0 +1,37 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method createIdMatrix(n) public static
DIM_ = 'DIMENSION'
m = 0 -- Indexed string to hold matrix; default value for all elements is zero
m[DIM_] = n
loop i = 1 to n -- NetRexx indexed strings don't have to start at zero
m[i, i] = 1 -- set this diagonal element to 1
end i
return m
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method displayIdMatrix(m) public static
DIM_ = 'DIMENSION'
if \m.exists(DIM_) then signal RuntimeException('Matrix dimension not set')
n = m[DIM_]
loop i = 1 to n
ol = ''
loop j = 1 To n
ol = ol m[i, j]
end j
say ol
end i
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) public static
parse arg n .
if n = '' then n = 5
say 'Identity Matrix of size' n
displayIdMatrix(createIdMatrix(n))
return

View file

@ -1,5 +1,6 @@
def identity(size):
matrix = [[0] * size] * size
matrix = [[0]*size for i in range(size)]
#matrix = [[0] * size] * size #Has a flaw. See http://stackoverflow.com/questions/240178/unexpected-feature-in-a-python-list-of-lists
for i in range(size):
matrix[i][i] = 1

View file

@ -1 +1,11 @@
np.mat(np.eye(size))
>>> def identity(size):
... return {(x, y):int(x == y) for x in range(size) for y in range(size)}
...
>>> size = 4
>>> matrix = identity(size)
>>> print('\n'.join(' '.join(str(matrix[(x, y)]) for x in range(size)) for y in range(size)))
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>>>

View file

@ -0,0 +1 @@
np.mat(np.eye(size))

View file

@ -0,0 +1 @@
diag(3)