Skip to content

Commit 4816c71

Browse files
authored
selectElements for numpy
1 parent 2b5d5bf commit 4816c71

File tree

2 files changed

+66
-19
lines changed

2 files changed

+66
-19
lines changed

libraries/MatrixLib.java

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ public static ArrayList<ArrayList<Object>> collect_15(Collection<Object> _l,Func
284284
return _results_15;
285285
}
286286

287+
public static ArrayList<ArrayList<Object>> collect_selectElements(Collection<Object> _l,Function<Double, Boolean> f)
288+
{ // Implements: m->collect( _r | MatrixLib.selectElements(_r->oclAsType(Sequence),f) )
289+
290+
ArrayList<ArrayList<Object>> _results_15 = new ArrayList<ArrayList<Object>>();
291+
for (Object _i : _l)
292+
{ Object _r = (Object) _i;
293+
_results_15.add(MatrixLib.selectElements(((ArrayList) _r),f));
294+
}
295+
return _results_15;
296+
}
297+
287298
public static ArrayList<Double> collect_16(Collection<Object> _l)
288299
{ // Implements: Sequence{}->collect( object_16_xx | 0.0 )
289300
ArrayList<Double> _results_16 = new ArrayList<Double>();
@@ -605,26 +616,43 @@ public static double prdMatrix(ArrayList m)
605616
return MatrixLib.prdMatrix(sq) * MatrixLib.prdMatrix(Ocl.tail(m));
606617

607618
}
608-
else { }
609619

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

612622
return Ocl.prddouble(dmat);
623+
}
613624

614625

615626

627+
public static ArrayList selectElements(ArrayList m,Function<Double, Boolean> f)
628+
{ ArrayList result;
629+
if ((m).size() == 0)
630+
{ return (new ArrayList<Double>()); }
631+
632+
if ((((Object) m.get(1 - 1)) instanceof ArrayList))
633+
{ return MatrixLib_Aux.collect_selectElements(m,f); }
634+
635+
ArrayList<Double> dmat = new ArrayList<Double>();
636+
637+
ArrayList<Object> _range7 = new ArrayList<Object>();
638+
_range7.addAll(m);
639+
for (int _i6 = 0; _i6 < _range7.size(); _i6++)
640+
{ Object x = (Object) _range7.get(_i6);
641+
double y = (double) x;
642+
if (f.apply(y))
643+
{ dmat = Ocl.append(dmat,y); }
644+
}
645+
return dmat;
616646
}
617647

618648

619649
public static ArrayList elementwiseApply(ArrayList m,Function<Double, Double> f)
620650
{ ArrayList result;
621-
if ((m).size() == 0)
622-
{ return (new ArrayList<Object>()); }
623-
else { }
651+
if ((m).size() == 0)
652+
{ return (new ArrayList<Double>()); }
624653

625-
if ((((Object) m.get(1 - 1)) instanceof ArrayList))
626-
{ return MatrixLib_Aux.collect_15(m,f); }
627-
else { }
654+
if ((((Object) m.get(1 - 1)) instanceof ArrayList))
655+
{ return MatrixLib_Aux.collect_15(m,f); }
628656

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

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

637-
dmat = Ocl.append(dmat,(f).apply(y));
638-
639-
640-
}
641-
return dmat;
642-
643-
644-
645-
665+
dmat = Ocl.append(dmat,(f).apply(y));
666+
}
667+
return dmat;
646668
}
647669

648-
649670
public static ArrayList elementwiseMult(ArrayList m,double x)
650671
{ ArrayList result;
651672
if ((m).size() == 0)
652673
{ return (new ArrayList<Object>()); }
653-
else { }
674+
654675

655676
if ((((Object) m.get(1 - 1)) instanceof ArrayList))
656677
{ return MatrixLib_Aux.collect_17(m,x); }
657-
else { }
678+
658679

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

libraries/matrixlib.km3

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,32 @@ class MatrixLib {
198198
);
199199
return dmat;
200200

201+
static query selectElements(m : Sequence,
202+
f : Function(double,boolean)) : Sequence
203+
pre: true
204+
post: true
205+
activity:
206+
if m->size() = 0
207+
then
208+
return Sequence{}
209+
else skip;
210+
211+
if m->at(1)->oclIsTypeOf(Sequence)
212+
then
213+
return m->collect( _r |
214+
MatrixLib.selectElements(_r->oclAsType(Sequence), f))
215+
else
216+
skip;
217+
218+
var dmat : Sequence(double) := Sequence{}->collect(0.0);
219+
220+
for x : m
221+
do
222+
(var y : double := x->oclAsType(double);
223+
if (f->apply(y)) then dmat := dmat->append(y)
224+
else skip
225+
);
226+
return dmat;
201227

202228
static query elementwiseMult(m : Sequence, x : double) : Sequence
203229
pre: true

0 commit comments

Comments
 (0)