diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-05-27 20:22:07 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-05-27 20:22:07 +0000 |
commit | 8f1bdd060645a82b24ed732f73a4516fb12b563b (patch) | |
tree | c36c3954de518fd0adfffd4017e5def94bae1e29 /src/jvm/clojure/lang/Numbers.java | |
parent | 697b69fe9c8f5e2e8d40382e70d4ddd490c4a9ee (diff) |
primitive vectors, in progress
Diffstat (limited to 'src/jvm/clojure/lang/Numbers.java')
-rw-r--r-- | src/jvm/clojure/lang/Numbers.java | 1470 |
1 files changed, 1406 insertions, 64 deletions
diff --git a/src/jvm/clojure/lang/Numbers.java b/src/jvm/clojure/lang/Numbers.java index 9a544f38..2ed3f117 100644 --- a/src/jvm/clojure/lang/Numbers.java +++ b/src/jvm/clojure/lang/Numbers.java @@ -15,6 +15,12 @@ package clojure.lang; import java.math.BigInteger; import java.math.BigDecimal; import java.math.MathContext; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.LinkedList; +import java.util.Arrays; public class Numbers{ @@ -1220,75 +1226,1411 @@ static BitOps bitOps(Object x){ return INTEGER_BITOPS; } -static public class I{ -static public int add(int x, int y) {return x + y;} -static public int subtract(int x, int y) {return x - y;} -static public int negate(int x) {return -x;} -static public int inc(int x) {return x+1;} -static public int dec(int x) {return x-1;} -static public int multiply(int x, int y) {return x * y;} -static public int divide(int x, int y) {return x / y;} -static public boolean equiv(int x, int y) {return x == y;} -static public boolean lt(int x, int y) {return x < y;} -static public boolean lte(int x, int y) {return x <= y;} -static public boolean gt(int x, int y) {return x > y;} -static public boolean gte(int x, int y) {return x >= y;} -static public boolean pos(int x) {return x > 0;} -static public boolean neg(int x) {return x < 0;} -static public boolean zero(int x) {return x == 0;} -} -static public class L{ -static public long add(long x, long y) {return x + y;} -static public long subtract(long x, long y) {return x - y;} -static public long negate(long x) {return -x;} -static public long inc(long x) {return x+1;} -static public long dec(long x) {return x-1;} -static public long multiply(long x, long y) {return x * y;} -static public long divide(long x, long y) {return x / y;} -static public boolean equiv(long x, long y) {return x == y;} -static public boolean lt(long x, long y) {return x < y;} -static public boolean lte(long x, long y) {return x <= y;} -static public boolean gt(long x, long y) {return x > y;} -static public boolean gte(long x, long y) {return x >= y;} -static public boolean pos(long x) {return x > 0;} -static public boolean neg(long x) {return x < 0;} -static public boolean zero(long x) {return x == 0;} -} +//final static ExecutorService executor = Executors.newCachedThreadPool(); +//static public int minChunk = 100; +//static int chunkSize(int alength){ +// return Math.max(alength / Runtime.getRuntime().availableProcessors(), minChunk); +//} + +// } +// else +// { +// LinkedList<Callable<Float>> ops = new LinkedList<Callable<Float>>(); +// for(int offset = 0;offset < xs.length;offset+=chunk) +// { +// final int start = offset; +// final int end = Math.min(xs.length, start + chunk); +// ops.add(new Callable<Float>(){ +// public Float call() throws Exception{ +// for(int i=start;i<end;i++) +// xs[i] += ys[i]; +// return null; +// }}); +// } +// executor.invokeAll(ops); +// } static public class F{ -static public float add(float x, float y) {return x + y;} -static public float subtract(float x, float y) {return x - y;} -static public float negate(float x) {return -x;} -static public float inc(float x) {return x+1;} -static public float dec(float x) {return x-1;} -static public float multiply(float x, float y) {return x * y;} -static public float divide(float x, float y) {return x / y;} -static public boolean equiv(float x, float y) {return x == y;} -static public boolean lt(float x, float y) {return x < y;} -static public boolean lte(float x, float y) {return x <= y;} -static public boolean gt(float x, float y) {return x > y;} -static public boolean gte(float x, float y) {return x >= y;} -static public boolean pos(float x) {return x > 0;} -static public boolean neg(float x) {return x < 0;} -static public boolean zero(float x) {return x == 0;} + static public float add(float x, float y) {return x + y;} + static public float subtract(float x, float y) {return x - y;} + static public float negate(float x) {return -x;} + static public float inc(float x) {return x+1;} + static public float dec(float x) {return x-1;} + static public float multiply(float x, float y) {return x * y;} + static public float divide(float x, float y) {return x / y;} + static public boolean equiv(float x, float y) {return x == y;} + static public boolean lt(float x, float y) {return x < y;} + static public boolean lte(float x, float y) {return x <= y;} + static public boolean gt(float x, float y) {return x > y;} + static public boolean gte(float x, float y) {return x >= y;} + static public boolean pos(float x) {return x > 0;} + static public boolean neg(float x) {return x < 0;} + static public boolean zero(float x) {return x == 0;} + + static public float[] array(int size, Object init){ + float[] ret = new float[size]; + if(init instanceof Number) + { + float f = ((Number)init).floatValue(); + for(int i=0;i<ret.length;i++) + ret[i] = f; + } + else + { + ISeq s = RT.seq(init); + for(int i = 0; i < size && s != null;i++,s=s.rest()) + ret[i] = ((Number)s.first()).floatValue(); + } + return ret; + } + + static public float[] vsadd(float[] x, float y){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] += y; + return xs; + } + + static public float[] vssub(float[] x, float y){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] -= y; + return xs; + } + + static public float[] vsdiv(float[] x, float y){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] /= y; + return xs; + } + + static public float[] vsmul(float[] x, float y){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= y; + return xs; + } + + static public float[] svdiv(float y, float[] x){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = y/xs[i]; + return xs; + } + + static public float[] vsmuladd(float[] x, float y, float[] zs){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y + zs[i]; + return xs; + } + + static public float[] vsmulsub(float[] x, float y, float[] zs){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y - zs[i]; + return xs; + } + + static public float[] vsmulsadd(float[] x, float y, float z){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y + z; + return xs; + } + + static public float[] vsmulssub(float[] x, float y, float z){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y - z; + return xs; + } + + static public float[] vabs(float[] x){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.abs(xs[i]); + return xs; + } + + static public float[] vnegabs(float[] x){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = -Math.abs(xs[i]); + return xs; + } + + static public float[] vneg(float[] x){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = -xs[i]; + return xs; + } + + static public float[] vsqr(float[] x){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= xs[i]; + return xs; + } + + static public float[] vsignedsqr(float[] x){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= Math.abs(xs[i]); + return xs; + } + + static public float[] vclip(float[] x, float low, float high){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + { + if(xs[i]<low) + xs[i] = low; + else if(xs[i]>high) + xs[i] = high; + } + return xs; + } + + static public IPersistentVector vclipcounts(float[] x, float low, float high){ + final float[] xs = x.clone(); + int lowc = 0; + int highc = 0; + + for(int i=0;i<xs.length;i++) + { + if(xs[i]<low) + { + ++lowc; + xs[i] = low; + } + else if(xs[i]>high) + { + ++highc; + xs[i] = high; + } + } + return RT.vector(xs,lowc,highc); + } + + static public float[] vthresh(float[] x, float thresh, float otherwise){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + { + if(xs[i]<thresh) + xs[i] = otherwise; + } + return xs; + } + + static public float[] vreverse(float[] x){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[xs.length - i - 1]; + return xs; + } + + static public float[] vrunningsum(float[] x){ + final float[] xs = x.clone(); + for(int i=1;i<xs.length;i++) + xs[i] = xs[i - 1] + xs[i]; + return xs; + } + + static public float[] vsort(float[] x){ + final float[] xs = x.clone(); + Arrays.sort(xs); + return xs; + } + + static public float vdot(float[] xs, float[] ys){ + float ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i] * ys[i]; + return ret; + } + + static public float vmax(float[] xs){ + if(xs.length == 0) + return 0; + float ret = xs[0]; + for(int i=0;i<xs.length;i++) + ret = Math.max(ret,xs[i]); + return ret; + } + + static public float vmin(float[] xs){ + if(xs.length == 0) + return 0; + float ret = xs[0]; + for(int i=0;i<xs.length;i++) + ret = Math.min(ret,xs[i]); + return ret; + } + + static public float vmean(float[] xs) { + if(xs.length == 0) + return 0; + return vsum(xs)/xs.length; + } + + static public double vrms(float[] xs) { + if(xs.length == 0) + return 0; + float ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i]*xs[i]; + return Math.sqrt(ret/xs.length); + } + + static public float vsum(float[] xs) { + float ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i]; + return ret; + } + + static public boolean vequiv(float[] xs, float[] ys){ + return Arrays.equals(xs,ys) ; + } + + static public float[] vadd(float[] x, float[] ys){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] += ys[i]; + return xs; + } + + static public float[] vsub(float[] x, float[] ys){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] -= ys[i]; + return xs; + } + + static public float[] vaddmul(float[] x, float[] ys, float[] zs){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]+ys[i])*zs[i]; + return xs; + } + + static public float[] vsubmul(float[] x, float[] ys, float[] zs){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]-ys[i])*zs[i]; + return xs; + } + + static public float[] vaddsmul(float[] x, float[] ys, float z){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]+ys[i])*z; + return xs; + } + + static public float[] vsubsmul(float[] x, float[] ys, float z){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]-ys[i])*z; + return xs; + } + + static public float[] vmulsadd(float[] x, float[] ys, float z){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])+z; + return xs; + } + + static public float[] vdiv(float[] x, float[] ys){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] /= ys[i]; + return xs; + } + + static public float[] vmul(float[] x, float[] ys){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= ys[i]; + return xs; + } + + static public float[] vmuladd(float[] x, float[] ys, float[] zs){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])+zs[i]; + return xs; + } + + static public float[] vmulsub(float[] x, float[] ys, float[] zs){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])-zs[i]; + return xs; + } + + static public float[] vmax(float[] x, float[] ys){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.max(xs[i],ys[i]); + return xs; + } + + static public float[] vmin(float[] x, float[] ys){ + final float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.min(xs[i],ys[i]); + return xs; + } + + static public float[] vmap(float[] x, IFn fn) throws Exception{ + float[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = ((Number)fn.invoke(xs[i])).floatValue(); + return xs; + } } static public class D{ -static public double add(double x, double y) {return x + y;} -static public double subtract(double x, double y) {return x - y;} -static public double negate(double x) {return -x;} -static public double inc(double x) {return x+1;} -static public double dec(double x) {return x-1;} -static public double multiply(double x, double y) {return x * y;} -static public double divide(double x, double y) {return x / y;} -static public boolean equiv(double x, double y) {return x == y;} -static public boolean lt(double x, double y) {return x < y;} -static public boolean lte(double x, double y) {return x <= y;} -static public boolean gt(double x, double y) {return x > y;} -static public boolean gte(double x, double y) {return x >= y;} -static public boolean pos(double x) {return x > 0;} -static public boolean neg(double x) {return x < 0;} -static public boolean zero(double x) {return x == 0;} + static public double add(double x, double y) {return x + y;} + static public double subtract(double x, double y) {return x - y;} + static public double negate(double x) {return -x;} + static public double inc(double x) {return x+1;} + static public double dec(double x) {return x-1;} + static public double multiply(double x, double y) {return x * y;} + static public double divide(double x, double y) {return x / y;} + static public boolean equiv(double x, double y) {return x == y;} + static public boolean lt(double x, double y) {return x < y;} + static public boolean lte(double x, double y) {return x <= y;} + static public boolean gt(double x, double y) {return x > y;} + static public boolean gte(double x, double y) {return x >= y;} + static public boolean pos(double x) {return x > 0;} + static public boolean neg(double x) {return x < 0;} + static public boolean zero(double x) {return x == 0;} + + static public double[] array(int size, Object init){ + double[] ret = new double[size]; + if(init instanceof Number) + { + double f = ((Number)init).doubleValue(); + for(int i=0;i<ret.length;i++) + ret[i] = f; + } + else + { + ISeq s = RT.seq(init); + for(int i = 0; i < size && s != null;i++,s=s.rest()) + ret[i] = ((Number)s.first()).doubleValue(); + } + return ret; + } + + static public double[] vsadd(double[] x, double y){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] += y; + return xs; + } + + static public double[] vssub(double[] x, double y){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] -= y; + return xs; + } + + static public double[] vsdiv(double[] x, double y){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] /= y; + return xs; + } + + static public double[] vsmul(double[] x, double y){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= y; + return xs; + } + + static public double[] svdiv(double y, double[] x){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = y/xs[i]; + return xs; + } + + static public double[] vsmuladd(double[] x, double y, double[] zs){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y + zs[i]; + return xs; + } + + static public double[] vsmulsub(double[] x, double y, double[] zs){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y - zs[i]; + return xs; + } + + static public double[] vsmulsadd(double[] x, double y, double z){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y + z; + return xs; + } + + static public double[] vsmulssub(double[] x, double y, double z){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y - z; + return xs; + } + + static public double[] vabs(double[] x){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.abs(xs[i]); + return xs; + } + + static public double[] vnegabs(double[] x){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = -Math.abs(xs[i]); + return xs; + } + + static public double[] vneg(double[] x){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = -xs[i]; + return xs; + } + + static public double[] vsqr(double[] x){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= xs[i]; + return xs; + } + + static public double[] vsignedsqr(double[] x){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= Math.abs(xs[i]); + return xs; + } + + static public double[] vclip(double[] x, double low, double high){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + { + if(xs[i]<low) + xs[i] = low; + else if(xs[i]>high) + xs[i] = high; + } + return xs; + } + + static public IPersistentVector vclipcounts(double[] x, double low, double high){ + final double[] xs = x.clone(); + int lowc = 0; + int highc = 0; + + for(int i=0;i<xs.length;i++) + { + if(xs[i]<low) + { + ++lowc; + xs[i] = low; + } + else if(xs[i]>high) + { + ++highc; + xs[i] = high; + } + } + return RT.vector(xs,lowc,highc); + } + + static public double[] vthresh(double[] x, double thresh, double otherwise){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + { + if(xs[i]<thresh) + xs[i] = otherwise; + } + return xs; + } + + static public double[] vreverse(double[] x){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[xs.length - i - 1]; + return xs; + } + + static public double[] vrunningsum(double[] x){ + final double[] xs = x.clone(); + for(int i=1;i<xs.length;i++) + xs[i] = xs[i - 1] + xs[i]; + return xs; + } + + static public double[] vsort(double[] x){ + final double[] xs = x.clone(); + Arrays.sort(xs); + return xs; + } + + static public double vdot(double[] xs, double[] ys){ + double ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i] * ys[i]; + return ret; + } + + static public double vmax(double[] xs){ + if(xs.length == 0) + return 0; + double ret = xs[0]; + for(int i=0;i<xs.length;i++) + ret = Math.max(ret,xs[i]); + return ret; + } + + static public double vmin(double[] xs){ + if(xs.length == 0) + return 0; + double ret = xs[0]; + for(int i=0;i<xs.length;i++) + ret = Math.min(ret,xs[i]); + return ret; + } + + static public double vmean(double[] xs) { + if(xs.length == 0) + return 0; + return vsum(xs)/xs.length; + } + + static public double vrms(double[] xs) { + if(xs.length == 0) + return 0; + double ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i]*xs[i]; + return Math.sqrt(ret/xs.length); + } + + static public double vsum(double[] xs) { + double ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i]; + return ret; + } + + static public boolean vequiv(double[] xs, double[] ys){ + return Arrays.equals(xs,ys) ; + } + + static public double[] vadd(double[] x, double[] ys){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] += ys[i]; + return xs; + } + + static public double[] vsub(double[] x, double[] ys){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] -= ys[i]; + return xs; + } + + static public double[] vaddmul(double[] x, double[] ys, double[] zs){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]+ys[i])*zs[i]; + return xs; + } + + static public double[] vsubmul(double[] x, double[] ys, double[] zs){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]-ys[i])*zs[i]; + return xs; + } + + static public double[] vaddsmul(double[] x, double[] ys, double z){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]+ys[i])*z; + return xs; + } + + static public double[] vsubsmul(double[] x, double[] ys, double z){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]-ys[i])*z; + return xs; + } + + static public double[] vmulsadd(double[] x, double[] ys, double z){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])+z; + return xs; + } + + static public double[] vdiv(double[] x, double[] ys){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] /= ys[i]; + return xs; + } + + static public double[] vmul(double[] x, double[] ys){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= ys[i]; + return xs; + } + + static public double[] vmuladd(double[] x, double[] ys, double[] zs){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])+zs[i]; + return xs; + } + + static public double[] vmulsub(double[] x, double[] ys, double[] zs){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])-zs[i]; + return xs; + } + + static public double[] vmax(double[] x, double[] ys){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.max(xs[i],ys[i]); + return xs; + } + + static public double[] vmin(double[] x, double[] ys){ + final double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.min(xs[i],ys[i]); + return xs; + } + + static public double[] vmap(double[] x, IFn fn) throws Exception{ + double[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = ((Number)fn.invoke(xs[i])).doubleValue(); + return xs; + } } + +static public class I{ + static public int add(int x, int y) {return x + y;} + static public int subtract(int x, int y) {return x - y;} + static public int negate(int x) {return -x;} + static public int inc(int x) {return x+1;} + static public int dec(int x) {return x-1;} + static public int multiply(int x, int y) {return x * y;} + static public int divide(int x, int y) {return x / y;} + static public boolean equiv(int x, int y) {return x == y;} + static public boolean lt(int x, int y) {return x < y;} + static public boolean lte(int x, int y) {return x <= y;} + static public boolean gt(int x, int y) {return x > y;} + static public boolean gte(int x, int y) {return x >= y;} + static public boolean pos(int x) {return x > 0;} + static public boolean neg(int x) {return x < 0;} + static public boolean zero(int x) {return x == 0;} + + static public int[] array(int size, Object init){ + int[] ret = new int[size]; + if(init instanceof Number) + { + int f = ((Number)init).intValue(); + for(int i=0;i<ret.length;i++) + ret[i] = f; + } + else + { + ISeq s = RT.seq(init); + for(int i = 0; i < size && s != null;i++,s=s.rest()) + ret[i] = ((Number)s.first()).intValue(); + } + return ret; + } + + static public int[] vsadd(int[] x, int y){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] += y; + return xs; + } + + static public int[] vssub(int[] x, int y){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] -= y; + return xs; + } + + static public int[] vsdiv(int[] x, int y){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] /= y; + return xs; + } + + static public int[] vsmul(int[] x, int y){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= y; + return xs; + } + + static public int[] svdiv(int y, int[] x){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = y/xs[i]; + return xs; + } + + static public int[] vsmuladd(int[] x, int y, int[] zs){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y + zs[i]; + return xs; + } + + static public int[] vsmulsub(int[] x, int y, int[] zs){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y - zs[i]; + return xs; + } + + static public int[] vsmulsadd(int[] x, int y, int z){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y + z; + return xs; + } + + static public int[] vsmulssub(int[] x, int y, int z){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[i]*y - z; + return xs; + } + + static public int[] vabs(int[] x){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.abs(xs[i]); + return xs; + } + + static public int[] vnegabs(int[] x){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = -Math.abs(xs[i]); + return xs; + } + + static public int[] vneg(int[] x){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = -xs[i]; + return xs; + } + + static public int[] vsqr(int[] x){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= xs[i]; + return xs; + } + + static public int[] vsignedsqr(int[] x){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= Math.abs(xs[i]); + return xs; + } + + static public int[] vclip(int[] x, int low, int high){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + { + if(xs[i]<low) + xs[i] = low; + else if(xs[i]>high) + xs[i] = high; + } + return xs; + } + + static public IPersistentVector vclipcounts(int[] x, int low, int high){ + final int[] xs = x.clone(); + int lowc = 0; + int highc = 0; + + for(int i=0;i<xs.length;i++) + { + if(xs[i]<low) + { + ++lowc; + xs[i] = low; + } + else if(xs[i]>high) + { + ++highc; + xs[i] = high; + } + } + return RT.vector(xs,lowc,highc); + } + + static public int[] vthresh(int[] x, int thresh, int otherwise){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + { + if(xs[i]<thresh) + xs[i] = otherwise; + } + return xs; + } + + static public int[] vreverse(int[] x){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = xs[xs.length - i - 1]; + return xs; + } + + static public int[] vrunningsum(int[] x){ + final int[] xs = x.clone(); + for(int i=1;i<xs.length;i++) + xs[i] = xs[i - 1] + xs[i]; + return xs; + } + + static public int[] vsort(int[] x){ + final int[] xs = x.clone(); + Arrays.sort(xs); + return xs; + } + + static public int vdot(int[] xs, int[] ys){ + int ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i] * ys[i]; + return ret; + } + + static public int vmax(int[] xs){ + if(xs.length == 0) + return 0; + int ret = xs[0]; + for(int i=0;i<xs.length;i++) + ret = Math.max(ret,xs[i]); + return ret; + } + + static public int vmin(int[] xs){ + if(xs.length == 0) + return 0; + int ret = xs[0]; + for(int i=0;i<xs.length;i++) + ret = Math.min(ret,xs[i]); + return ret; + } + + static public double vmean(int[] xs) { + if(xs.length == 0) + return 0; + return vsum(xs)/(double)xs.length; + } + + static public double vrms(int[] xs) { + if(xs.length == 0) + return 0; + int ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i]*xs[i]; + return Math.sqrt(ret/(double)xs.length); + } + + static public int vsum(int[] xs) { + int ret = 0; + for(int i=0;i<xs.length;i++) + ret += xs[i]; + return ret; + } + + static public boolean vequiv(int[] xs, int[] ys){ + return Arrays.equals(xs,ys) ; + } + + static public int[] vadd(int[] x, int[] ys){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] += ys[i]; + return xs; + } + + static public int[] vsub(int[] x, int[] ys){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] -= ys[i]; + return xs; + } + + static public int[] vaddmul(int[] x, int[] ys, int[] zs){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]+ys[i])*zs[i]; + return xs; + } + + static public int[] vsubmul(int[] x, int[] ys, int[] zs){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]-ys[i])*zs[i]; + return xs; + } + + static public int[] vaddsmul(int[] x, int[] ys, int z){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]+ys[i])*z; + return xs; + } + + static public int[] vsubsmul(int[] x, int[] ys, int z){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]-ys[i])*z; + return xs; + } + + static public int[] vmulsadd(int[] x, int[] ys, int z){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])+z; + return xs; + } + + static public int[] vdiv(int[] x, int[] ys){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] /= ys[i]; + return xs; + } + + static public int[] vmul(int[] x, int[] ys){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] *= ys[i]; + return xs; + } + + static public int[] vmuladd(int[] x, int[] ys, int[] zs){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])+zs[i]; + return xs; + } + + static public int[] vmulsub(int[] x, int[] ys, int[] zs){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = (xs[i]*ys[i])-zs[i]; + return xs; + } + + static public int[] vmax(int[] x, int[] ys){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.max(xs[i],ys[i]); + return xs; + } + + static public int[] vmin(int[] x, int[] ys){ + final int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = Math.min(xs[i],ys[i]); + return xs; + } + + static public int[] vmap(int[] x, IFn fn) throws Exception{ + int[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] = ((Number)fn.invoke(xs[i])).intValue(); + return xs; + } + +} + +static public class L{ + static public long add(long x, long y) {return x + y;} + static public long subtract(long x, long y) {return x - y;} + static public long negate(long x) {return -x;} + static public long inc(long x) {return x+1;} + static public long dec(long x) {return x-1;} + static public long multiply(long x, long y) {return x * y;} + static public long divide(long x, long y) {return x / y;} + static public boolean equiv(long x, long y) {return x == y;} + static public boolean lt(long x, long y) {return x < y;} + static public boolean lte(long x, long y) {return x <= y;} + static public boolean gt(long x, long y) {return x > y;} + static public boolean gte(long x, long y) {return x >= y;} + static public boolean pos(long x) {return x > 0;} + static public boolean neg(long x) {return x < 0;} + static public boolean zero(long x) {return x == 0;} + + static public long[] array(int size, Object init){ + long[] ret = new long[size]; + if(init instanceof Number) + { + long f = ((Number)init).longValue(); + for(int i=0;i<ret.length;i++) + ret[i] = f; + } + else + { + ISeq s = RT.seq(init); + for(int i = 0; i < size && s != null;i++,s=s.rest()) + ret[i] = ((Number)s.first()).longValue(); + } + return ret; + } + + static public long[] vsadd(long[] x, long y){ + final long[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] += y; + return xs; + } + + static public long[] vssub(long[] x, long y){ + final long[] xs = x.clone(); + for(int i=0;i<xs.length;i++) + xs[i] -= y; + return xs; |