Skip to content

Commit

Permalink
Improved PCG and matrixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Sep 20, 2024
1 parent 5545a58 commit ad5f2e3
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 93 deletions.
70 changes: 68 additions & 2 deletions libraries/OclRandom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void setiz(int izval)

public void setAlgorithm(string algo)
{ if ("PCG".Equals(algo))
{ pcg = new Pcg(); }
{ pcg = new Pcg((ulong) SystemTypes.getTime() , 0xda3e39cb94b95bdbUL); }
algorithm = algo;
}

Expand Down Expand Up @@ -272,7 +272,7 @@ public void setAlgorithm(string algo)
public double getpoissonLambda() { return poissonLambda; }


public static OclRandom newOclRandom()
public static OclRandom newOclRandom()
{
OclRandom result = null;

Expand All @@ -285,6 +285,21 @@ public static OclRandom newOclRandom()
return result;
}

public static OclRandom newOclRandom_PCG()
{
OclRandom result = null;

OclRandom rd = new OclRandom();
rd.setix(1001);
rd.setiy(781);
rd.setiz(913);
rd.setdistribution("uniform");
rd.algorithm = "PCG";
rd.pcg = new Pcg((ulong) SystemTypes.getTime() , 0xda3e39cb94b95bdbUL);
result = rd;
return result;
}


public static OclRandom newOclRandom(long n)
{
Expand Down Expand Up @@ -725,4 +740,55 @@ public static ArrayList randomElements(ArrayList col, int n)
return res;
}

public static ArrayList randomList(int n, OclRandom rd)
{ // Implements: Integer.subrange(1,sh->at(1))->collect( int_0_xx | rd.nextDouble() )

ArrayList _results_0 = new ArrayList();
for (int _i = 0; _i < n; _i++)
{ _results_0.Add(rd.nextDouble()); }

return _results_0;
}

public static ArrayList randomMatrix(int n,
ArrayList sh, OclRandom rd)
{ // Implements: Integer.subrange(1,sh->at(1))->collect( int_1_xx | OclRandom.randomValuesMatrix(sh->tail()) )

ArrayList _results_1 = new ArrayList();
for (int _i = 0; _i < n; _i++)
{
_results_1.Add(
OclRandom.randomValuesMatrix(
SystemTypes.tail(sh), rd));
}

return _results_1;
}

public static ArrayList randomValuesMatrix(ArrayList sh)
{
OclRandom rd = OclRandom.newOclRandom_PCG();

if (sh.Count == 0)
{ return (new ArrayList()); }

if ((sh).Count == 1)
{ return OclRandom.randomList((int)sh[0], rd); }

ArrayList res = OclRandom.randomMatrix((int)sh[0], sh, rd);
return res;
}

public static ArrayList randomValuesMatrix(ArrayList sh, OclRandom rd)
{
if (sh.Count == 0)
{ return (new ArrayList()); }

if ((sh).Count == 1)
{ return OclRandom.randomList((int)sh[0], rd); }

ArrayList res = OclRandom.randomMatrix((int)sh[0], sh, rd);
return res;
}

}
87 changes: 50 additions & 37 deletions libraries/OclRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ public static OclRandom newOclRandom(long n)
return result;
}

public static OclRandom newOclRandom_PCG()
{
OclRandom result = null;
OclRandom rd = null;
rd = OclRandom.createOclRandom();
rd.ix = 1001;
rd.iy = 781;
rd.iz = 913;
rd.pcg = new Pcg32();
rd.algorithm = "PCG";
result = rd;
return result;
}

public static OclRandom newOclRandom_Seed(long n)
{
OclRandom result = null;
Expand Down Expand Up @@ -484,54 +498,53 @@ public static ArrayList randomUniqueElements(ArrayList col, int n)
}
return res;
}

/*
public static void main(String[] args)
{ OclRandom lcg = OclRandom.newOclRandom();

System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
public static ArrayList<Object> randomList(int n, OclRandom rd)
{ // Implements: Integer.subrange(1,sh->at(1))->collect( int_0_xx | rd.nextDouble() )

System.out.println();
ArrayList<Object> _results_0 = new ArrayList<Object>();
for (int _i = 0; _i < n; _i++)
{ _results_0.add(new Double(rd.nextDouble())); }

System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
return _results_0;
}

System.out.println();
public static ArrayList<Object> randomMatrix(int n,
ArrayList<Integer> sh)
{ // Implements: Integer.subrange(1,sh->at(1))->collect( int_1_xx | OclRandom.randomValuesMatrix(sh->tail()) )

System.out.println(lcg.nextGaussian());
System.out.println(lcg.nextGaussian());
System.out.println(lcg.nextGaussian());
ArrayList<Object> _results_1 = new ArrayList<Object>();
for (int _i = 0; _i < n; _i++)
{ _results_1.add(
OclRandom.randomValuesMatrix(Ocl.tail(sh)));
}

System.out.println();
return _results_1;
}

lcg.setAlgorithm("PCG");
System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
System.out.println(lcg.nextInt());
public static ArrayList<Object> randomValuesMatrix(ArrayList<Integer> sh)
{ ArrayList<Object> result;
OclRandom rd = OclRandom.newOclRandom_PCG();

System.out.println();
if ((sh).size() == 0)
{ return (new ArrayList<Object>()); }

System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
System.out.println(lcg.nextDouble());
if ((sh).size() == 1)
{ return OclRandom.randomList(sh.get(0), rd); }

System.out.println();
ArrayList<Object> res =
OclRandom.randomMatrix(sh.get(0), sh);
return res;
}

System.out.println(lcg.nextGaussian());
System.out.println(lcg.nextGaussian());
System.out.println(lcg.nextGaussian());
} */

/*
public static void main(String[] args)
{ ArrayList<Integer> sh = new ArrayList<Integer>();
sh.add(2); sh.add(3);
ArrayList mtrx = OclRandom.randomValuesMatrix(sh);
System.out.println(mtrx);
} */
}


Expand Down
41 changes: 31 additions & 10 deletions libraries/matrixlib.km3
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class MatrixLib {

static query matrixMultiplication(m1: Sequence(Sequence(double)), m2 : Sequence(Sequence(double))) : Sequence(Sequence(double))
pre: true
post: result = m1->collect( row | MathLib.rowMult(row, m2) );
post: result = m1->collect( row | MatrixLib.rowMult(row, m2) );

static query subRows(m : Sequence(OclAny), s : Sequence(int)) : Sequence(OclAny)
pre: true
Expand Down Expand Up @@ -60,7 +60,7 @@ class MatrixLib {
return res ;
return res;

static operation singleValueMatrix(sh : Sequence(int), x : double) : Sequence(OclAny)
static query singleValueMatrix(sh : Sequence(int), x : OclAny) : Sequence(OclAny)
pre: true
post: true
activity:
Expand All @@ -78,7 +78,8 @@ class MatrixLib {
res := Integer.subrange(1,sh->at(1))->collect(MatrixLib.singleValueMatrix(sh->tail(), x)) ;
return res;

static operation fillMatrixFrom(sq : Sequence(double), sh : Sequence(int)) : Sequence(OclAny)

static query fillMatrixFrom(sq : Sequence(double), sh : Sequence(int)) : Sequence(OclAny)
pre: true
post: true
activity:
Expand Down Expand Up @@ -108,7 +109,7 @@ class MatrixLib {
post:
result = Integer.subrange(1,n)->collect( i | Integer.subrange(1,n)->collect( j | if i = j then 1.0 else 0.0 endif ) );

static query flattenMatrix(m : Sequence(OclAny)) : Sequence(double)
static query flattenMatrix(m : Sequence(OclAny)) : Sequence(OclAny)
pre: true
post: true
activity:
Expand Down Expand Up @@ -153,7 +154,7 @@ class MatrixLib {
else
return m->prd();

static operation elementwiseApply(m : Sequence(OclAny), f : Function(double,double)) : Sequence(OclAny)
static query elementwiseApply(m : Sequence(OclAny), f : Function(double,double)) : Sequence(OclAny)
pre: true
post: true
activity:
Expand All @@ -172,7 +173,7 @@ class MatrixLib {
return m->collect( _elem | f->apply(_elem->oclAsType(double)));


static operation elementwiseMult(m : Sequence(OclAny), x : double) : Sequence(OclAny)
static query elementwiseMult(m : Sequence(OclAny), x : double) : Sequence(OclAny)
pre: true
post: true
activity:
Expand All @@ -190,7 +191,27 @@ class MatrixLib {

return m->collect( _elem | (_elem->oclAsType(double))*x )

static operation elementwiseDivide(m : Sequence(OclAny), x : double) : Sequence(OclAny)

static query elementwiseAdd(m : Sequence(OclAny), x : double) : Sequence(OclAny)
pre: true
post: true
activity:
if m->size() = 0
then
return Sequence{}
else skip;

if m->at(1)->oclIsTypeOf(Sequence)
then
return m->collect( _r |
MatrixLib.elementwiseAdd(_r, x))
else
skip;

return m->collect( _elem | (_elem->oclAsType(double)) + x )


static query elementwiseDivide(m : Sequence(OclAny), x : double) : Sequence(OclAny)
pre: true
post: true
activity:
Expand All @@ -206,7 +227,7 @@ class MatrixLib {
else
skip;

static operation elementwiseLess(m : Sequence(OclAny), x : double) : Sequence(OclAny)
static query elementwiseLess(m : Sequence(OclAny), x : double) : Sequence(OclAny)
pre: true
post: true
activity:
Expand All @@ -224,7 +245,7 @@ class MatrixLib {

return m->collect( _elem | if (_elem->oclAsType(double)) < x then true else false endif )

static operation elementwiseGreater(m : Sequence(OclAny), x : double) : Sequence(OclAny)
static query elementwiseGreater(m : Sequence(OclAny), x : double) : Sequence(OclAny)
pre: true
post: true
activity:
Expand All @@ -243,7 +264,7 @@ class MatrixLib {
return m->collect( _elem | if (_elem->oclAsType(double)) > x then true else false endif )


static operation elementwiseEqual(m : Sequence(OclAny), x : double) : Sequence(OclAny)
static query elementwiseEqual(m : Sequence(OclAny), x : double) : Sequence(OclAny)
pre: true
post: true
activity:
Expand Down
25 changes: 25 additions & 0 deletions libraries/oclrandom.km3
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ package oclrandom {
pre: true
post: OclRandom->exists( rd | rd.ix = 1001 & rd.iy = 781 & rd.iz = 913 & rd.distribution = "uniform" & result = rd );

static operation newOclRandom_PCG() : OclRandom
pre: true
post: OclRandom->exists( rd | rd.ix = 1001 & rd.iy = 781 & rd.iz = 913 & rd.algorithm = "PCG" & rd.distribution = "uniform" & result = rd );

static operation newOclRandom_Seed(n : long) : OclRandom
pre: true
post: OclRandom->exists( rd | rd.ix = (n mod 30269)->oclAsType(int) & rd.iy = (n mod 30307)->oclAsType(int) & rd.iz = (n mod 30323)->oclAsType(int) & rd.distribution = "uniform" & result = rd );
Expand Down Expand Up @@ -225,6 +229,27 @@ package oclrandom {

static operation randomUniqueElements(sq : Sequence, n : int) : Sequence
pre: true post: true;

static operation randomValuesMatrix(sh : Sequence(int)) : Sequence(OclAny)
pre: true
post: true
activity:
var rd : OclRandom := OclRandom.newOclRandom_PCG() ;

if sh->size() = 0
then
return Sequence{}
else skip ;

if sh->size() = 1
then
return Integer.subrange(1,sh->at(1))->collect(rd.nextDouble())
else skip ;

var res : Sequence(OclAny) ;
res := Integer.subrange(1,sh->at(1))->collect(OclRandom.randomValuesMatrix(sh->tail())) ;
return res;

}
}

Expand Down
Loading

0 comments on commit ad5f2e3

Please sign in to comment.