Skip to content

Commit

Permalink
selectElements for numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlano authored Dec 30, 2024
1 parent 2b5d5bf commit 4816c71
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
59 changes: 40 additions & 19 deletions libraries/MatrixLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,17 @@ public static ArrayList<ArrayList<Object>> collect_15(Collection<Object> _l,Func
return _results_15;
}

public static ArrayList<ArrayList<Object>> collect_selectElements(Collection<Object> _l,Function<Double, Boolean> f)
{ // Implements: m->collect( _r | MatrixLib.selectElements(_r->oclAsType(Sequence),f) )

ArrayList<ArrayList<Object>> _results_15 = new ArrayList<ArrayList<Object>>();
for (Object _i : _l)
{ Object _r = (Object) _i;
_results_15.add(MatrixLib.selectElements(((ArrayList) _r),f));
}
return _results_15;
}

public static ArrayList<Double> collect_16(Collection<Object> _l)
{ // Implements: Sequence{}->collect( object_16_xx | 0.0 )
ArrayList<Double> _results_16 = new ArrayList<Double>();
Expand Down Expand Up @@ -605,26 +616,43 @@ public static double prdMatrix(ArrayList m)
return MatrixLib.prdMatrix(sq) * MatrixLib.prdMatrix(Ocl.tail(m));

}
else { }

ArrayList<Double> dmat = Ocl.concatenate(Ocl.addSequence((new ArrayList<Double>()), new Double(1.0)), m);

return Ocl.prddouble(dmat);
}



public static ArrayList selectElements(ArrayList m,Function<Double, Boolean> f)
{ ArrayList result;
if ((m).size() == 0)
{ return (new ArrayList<Double>()); }

if ((((Object) m.get(1 - 1)) instanceof ArrayList))
{ return MatrixLib_Aux.collect_selectElements(m,f); }

ArrayList<Double> dmat = new ArrayList<Double>();

ArrayList<Object> _range7 = new ArrayList<Object>();
_range7.addAll(m);
for (int _i6 = 0; _i6 < _range7.size(); _i6++)
{ Object x = (Object) _range7.get(_i6);
double y = (double) x;
if (f.apply(y))
{ dmat = Ocl.append(dmat,y); }
}
return dmat;
}


public static ArrayList elementwiseApply(ArrayList m,Function<Double, Double> f)
{ ArrayList result;
if ((m).size() == 0)
{ return (new ArrayList<Object>()); }
else { }
if ((m).size() == 0)
{ return (new ArrayList<Double>()); }

if ((((Object) m.get(1 - 1)) instanceof ArrayList))
{ return MatrixLib_Aux.collect_15(m,f); }
else { }
if ((((Object) m.get(1 - 1)) instanceof ArrayList))
{ return MatrixLib_Aux.collect_15(m,f); }

ArrayList<Double> dmat = MatrixLib_Aux.collect_16((new ArrayList<Object>()));

Expand All @@ -634,27 +662,20 @@ public static ArrayList elementwiseApply(ArrayList m,Function<Double, Double> f)
{ Object x = (Object) _range7.get(_i6);
double y = ((double) x);

dmat = Ocl.append(dmat,(f).apply(y));


}
return dmat;




dmat = Ocl.append(dmat,(f).apply(y));
}
return dmat;
}


public static ArrayList elementwiseMult(ArrayList m,double x)
{ ArrayList result;
if ((m).size() == 0)
{ return (new ArrayList<Object>()); }
else { }


if ((((Object) m.get(1 - 1)) instanceof ArrayList))
{ return MatrixLib_Aux.collect_17(m,x); }
else { }


ArrayList<Double> dmat = MatrixLib_Aux.collect_16((new ArrayList<Object>()));

Expand Down
26 changes: 26 additions & 0 deletions libraries/matrixlib.km3
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,32 @@ class MatrixLib {
);
return dmat;

static query selectElements(m : Sequence,
f : Function(double,boolean)) : Sequence
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.selectElements(_r->oclAsType(Sequence), f))
else
skip;

var dmat : Sequence(double) := Sequence{}->collect(0.0);

for x : m
do
(var y : double := x->oclAsType(double);
if (f->apply(y)) then dmat := dmat->append(y)
else skip
);
return dmat;

static query elementwiseMult(m : Sequence, x : double) : Sequence
pre: true
Expand Down

0 comments on commit 4816c71

Please sign in to comment.