diff options
-rw-r--r-- | src/boot.clj | 20 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Compiler.java | 148 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Numbers.java | 1442 | ||||
-rw-r--r-- | src/jvm/clojure/lang/RT.java | 71 | ||||
-rw-r--r-- | src/jvm/clojure/lang/Util.java | 5 |
5 files changed, 1138 insertions, 548 deletions
diff --git a/src/boot.clj b/src/boot.clj index 43b94bd7..96ab8804 100644 --- a/src/boot.clj +++ b/src/boot.clj @@ -458,6 +458,8 @@ ;;math stuff (defn + "Returns the sum of nums. (+) returns 0." + {:inline (fn [x y] `(. clojure.lang.Numbers (add ~x ~y))) + :inline-arities #{2}} ([] 0) ([x] (cast Number x)) ([x y] (. clojure.lang.Numbers (add x y))) @@ -1434,24 +1436,34 @@ not-every? (comp not every?)) (prn (str "Elapsed time: " (/ (- (. System (nanoTime)) start#) 1000000.0) " msecs")) ret#)) +(defn num + "Coerce to Number" + {:tag Number + :inline (fn [x] `(. clojure.lang.Numbers (num ~x)))} + [x] (. clojure.lang.Numbers (num x))) + (defn int "Coerce to int" - {:tag Integer} + {:tag Integer + :inline (fn [x] `(. clojure.lang.RT (intCast ~x)))} [x] (. clojure.lang.RT (intCast x))) (defn long "Coerce to long" - {:tag Long} + {:tag Long + :inline (fn [x] `(. clojure.lang.RT (longCast ~x)))} [#^Number x] (. x (longValue))) (defn float "Coerce to float" - {:tag Float} + {:tag Float + :inline (fn [x] `(. clojure.lang.RT (floatCast ~x)))} [#^Number x] (. x (floatValue))) (defn double "Coerce to double" - {:tag Double} + {:tag Double + :inline (fn [x] `(. clojure.lang.RT (doubleCast ~x)))} [#^Number x] (. x (doubleValue))) (defn short diff --git a/src/jvm/clojure/lang/Compiler.java b/src/jvm/clojure/lang/Compiler.java index c5296269..99c7b660 100644 --- a/src/jvm/clojure/lang/Compiler.java +++ b/src/jvm/clojure/lang/Compiler.java @@ -12,13 +12,13 @@ package clojure.lang; -//* +/* import clojure.asm.*; import clojure.asm.commons.Method; import clojure.asm.commons.GeneratorAdapter; /*/ -/* +//* import org.objectweb.asm.*; import org.objectweb.asm.commons.Method; @@ -70,6 +70,7 @@ static final Symbol _AMP_ = Symbol.create("&"); static final Symbol ISEQ = Symbol.create("clojure.lang.ISeq"); static final Keyword inlineKey = Keyword.intern(null, "inline"); +static final Keyword inlineAritiesKey = Keyword.intern(null, "inline-arities"); //static final Symbol IMPORT = Symbol.create("import"); //static final Symbol USE = Symbol.create("use"); @@ -491,7 +492,11 @@ static interface AssignableExpr{ void emitAssign(C context, FnExpr fn, GeneratorAdapter gen, Expr val); } -static public abstract class HostExpr implements Expr{ +static public interface MaybePrimitiveExpr{ + public void emitUnboxed(C context, FnExpr fn, GeneratorAdapter gen); +} + +static public abstract class HostExpr implements Expr, MaybePrimitiveExpr{ final static Type BOOLEAN_TYPE = Type.getType(Boolean.class); final static Type CHAR_TYPE = Type.getType(Character.class); final static Type INTEGER_TYPE = Type.getType(Integer.class); @@ -524,7 +529,6 @@ static public abstract class HostExpr implements Expr{ final static Method fromLongMethod = Method.getMethod("clojure.lang.Num from(long)"); final static Method fromDoubleMethod = Method.getMethod("clojure.lang.Num from(double)"); - abstract public void emitUnboxed(C context, FnExpr fn, GeneratorAdapter gen); /* public static void emitBoxReturn(FnExpr fn, GeneratorAdapter gen, Class returnType){ @@ -729,6 +733,15 @@ static public abstract class HostExpr implements Expr{ Symbol sym = (Symbol) form; if(sym.ns == null) //if ns-qualified can't be classname { +// if(sym.name.equals("int")) +// c = int.class; +// else if(sym.name.equals("long")) +// c = long.class; +// else if(sym.name.equals("float")) +// c = float.class; +// else if(sym.name.equals("double")) +// c = double.class; +// else if(sym.name.indexOf('.') > 0 || sym.name.charAt(0) == '[') c = RT.classForName(sym.name); else @@ -946,6 +959,23 @@ static class StaticFieldExpr extends FieldExpr implements AssignableExpr{ } +static Class maybePrimitiveType(Expr e){ + try + { + if(e instanceof MaybePrimitiveExpr && e.hasJavaClass()) + { + Class c = e.getJavaClass(); + if(Util.isPrimitive(c)) + return c; + } + } + catch(Exception ex) + { + throw new RuntimeException(ex); + } + return null; +} + static abstract class MethodExpr extends HostExpr{ static void emitArgsAsArray(IPersistentVector args, FnExpr fn, GeneratorAdapter gen){ gen.push(args.count()); @@ -965,9 +995,9 @@ static abstract class MethodExpr extends HostExpr{ Expr e = (Expr) args.nth(i); try { - if(e instanceof HostExpr && e.hasJavaClass() && e.getJavaClass() == parameterTypes[i]) + if(maybePrimitiveType(e) == parameterTypes[i]) { - ((HostExpr)e).emitUnboxed(C.EXPRESSION, fn, gen); + ((MaybePrimitiveExpr) e).emitUnboxed(C.EXPRESSION, fn, gen); } else { @@ -1187,7 +1217,7 @@ static class StaticMethodExpr extends MethodExpr{ gen.invokeStatic(type, m); } else - throw new UnsupportedOperationException("Unboxed emit of unknown member"); + throw new UnsupportedOperationException("Unboxed emit of unknown member"); } public void emit(C context, FnExpr fn, GeneratorAdapter gen){ @@ -1845,7 +1875,8 @@ static boolean subsumes(Class[] c1, Class[] c2){ if(!(c1[i] == c2[i] || c2[i].isPrimitive() && c1[i] == Object.class)) { if(c1[i].isPrimitive() && c2[i] == Object.class - || c2[i].isAssignableFrom(c1[i])) +// || Number.class.isAssignableFrom(c1[i]) && c2[i].isPrimitive() +|| c2[i].isAssignableFrom(c1[i])) better = true; else return false; @@ -2166,10 +2197,11 @@ static class IfExpr implements Expr{ try { - if(testExpr instanceof HostExpr && testExpr.hasJavaClass() && testExpr.getJavaClass() == boolean.class) + if(testExpr instanceof MaybePrimitiveExpr && testExpr.hasJavaClass() && + testExpr.getJavaClass() == boolean.class) { - ((HostExpr)testExpr).emitUnboxed(C.EXPRESSION, fn, gen); - gen.ifZCmp(gen.EQ,falseLabel); + ((MaybePrimitiveExpr) testExpr).emitUnboxed(C.EXPRESSION, fn, gen); + gen.ifZCmp(gen.EQ, falseLabel); } else { @@ -2711,8 +2743,8 @@ static public class FnExpr implements Expr{ //derived from AFn/RestFn ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); // ClassWriter cw = new ClassWriter(0); - ClassVisitor cv = cw; - //ClassVisitor cv = new TraceClassVisitor(new CheckClassAdapter(cw), new PrintWriter(System.out)); + //ClassVisitor cv = cw; + ClassVisitor cv = new TraceClassVisitor(new CheckClassAdapter(cw), new PrintWriter(System.out)); //ClassVisitor cv = new TraceClassVisitor(cw, new PrintWriter(System.out)); cv.visit(V1_5, ACC_PUBLIC, internalName, null, isVariadic() ? "clojure/lang/RestFn" : "clojure/lang/AFn", null); String source = (String) SOURCE.get(); @@ -2893,7 +2925,16 @@ static public class FnExpr implements Expr{ gen.getField(fntype, lb.name, OBJECT_TYPE); } else - gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ILOAD), lb.idx); + { + Class primc = lb.getPrimitiveType(); + if(primc != null) + { + gen.visitVarInsn(Type.getType(primc).getOpcode(Opcodes.ILOAD), lb.idx); + HostExpr.emitBoxReturn(this, gen, primc); + } + else + gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ILOAD), lb.idx); + } } public void emitVar(GeneratorAdapter gen, Var var){ @@ -3071,6 +3112,7 @@ static class FnMethod{ } void emitClearLocals(GeneratorAdapter gen){ + //this seems shaky given primitive locals - revisit for(int i = 1; i < numParams() + 1; i++) { if(!localsUsedInCatchFinally.contains(i)) @@ -3098,7 +3140,9 @@ static class LocalBinding{ final int idx; final String name; - public LocalBinding(int num, Symbol sym, Symbol tag, Expr init){ + public LocalBinding(int num, Symbol sym, Symbol tag, Expr init) throws Exception{ + if(maybePrimitiveType(init) != null && tag != null) + throw new UnsupportedOperationException("Can't type hint a local with a primitive initializer"); this.idx = num; this.sym = sym; this.tag = tag; @@ -3115,13 +3159,19 @@ static class LocalBinding{ return tag != null ? HostExpr.tagToClass(tag) : init.getJavaClass(); } + + public Class getPrimitiveType(){ + return maybePrimitiveType(init); + } } -static class LocalBindingExpr implements Expr{ +static class LocalBindingExpr implements Expr, MaybePrimitiveExpr{ final LocalBinding b; final Symbol tag; - public LocalBindingExpr(LocalBinding b, Symbol tag){ + public LocalBindingExpr(LocalBinding b, Symbol tag) throws Exception{ + if(b.getPrimitiveType() != null && tag != null) + throw new UnsupportedOperationException("Can't type hint a primitive local"); this.b = b; this.tag = tag; } @@ -3130,6 +3180,10 @@ static class LocalBindingExpr implements Expr{ throw new UnsupportedOperationException("Can't eval locals"); } + public void emitUnboxed(C context, FnExpr fn, GeneratorAdapter gen){ + gen.visitVarInsn(Type.getType(b.getPrimitiveType()).getOpcode(Opcodes.ILOAD), b.idx); + } + public void emit(C context, FnExpr fn, GeneratorAdapter gen){ if(context != C.STATEMENT) fn.emitLocal(gen, b); @@ -3144,6 +3198,8 @@ static class LocalBindingExpr implements Expr{ return HostExpr.tagToClass(tag); return b.getJavaClass(); } + + } static class BodyExpr implements Expr{ @@ -3293,8 +3349,17 @@ static class LetExpr implements Expr{ for(int i = 0; i < bindingInits.count(); i++) { BindingInit bi = (BindingInit) bindingInits.nth(i); - bi.init.emit(C.EXPRESSION, fn, gen); - gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ISTORE), bi.binding.idx); + Class primc = maybePrimitiveType(bi.init); + if(primc != null) + { + ((MaybePrimitiveExpr) bi.init).emitUnboxed(C.EXPRESSION, fn, gen); + gen.visitVarInsn(Type.getType(primc).getOpcode(Opcodes.ISTORE), bi.binding.idx); + } + else + { + bi.init.emit(C.EXPRESSION, fn, gen); + gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ISTORE), bi.binding.idx); + } } Label loopLabel = gen.mark(); if(isLoop) @@ -3316,7 +3381,12 @@ static class LetExpr implements Expr{ for(ISeq bis = bindingInits.seq(); bis != null; bis = bis.rest()) { BindingInit bi = (BindingInit) bis.first(); - gen.visitLocalVariable(bi.binding.name, "Ljava/lang/Object;", null, loopLabel, end, bi.binding.idx); + Class primc = maybePrimitiveType(bi.init); + if(primc != null) + gen.visitLocalVariable(bi.binding.name, Type.getDescriptor(primc), null, loopLabel, end, + bi.binding.idx); + else + gen.visitLocalVariable(bi.binding.name, "Ljava/lang/Object;", null, loopLabel, end, bi.binding.idx); } } @@ -3350,13 +3420,35 @@ static class RecurExpr implements Expr{ { LocalBinding lb = (LocalBinding) loopLocals.nth(i); Expr arg = (Expr) args.nth(i); - arg.emit(C.EXPRESSION, fn, gen); + if(lb.getPrimitiveType() != null) + { + Class primc = lb.getPrimitiveType(); + try + { + if(!(arg instanceof MaybePrimitiveExpr && arg.hasJavaClass() && arg.getJavaClass() == primc)) + throw new IllegalArgumentException("recur arg for primitive local: " + + lb.name + " must be matching primitive"); + } + catch(Exception e) + { + throw new RuntimeException(e); + } + ((MaybePrimitiveExpr) arg).emitUnboxed(C.EXPRESSION, fn, gen); + } + else + { + arg.emit(C.EXPRESSION, fn, gen); + } } for(int i = loopLocals.count() - 1; i >= 0; i--) { LocalBinding lb = (LocalBinding) loopLocals.nth(i); - gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ISTORE), lb.idx); + Class primc = lb.getPrimitiveType(); + if(primc != null) + gen.visitVarInsn(Type.getType(primc).getOpcode(Opcodes.ISTORE), lb.idx); + else + gen.visitVarInsn(OBJECT_TYPE.getOpcode(Opcodes.ISTORE), lb.idx); } gen.goTo(loopLabel); @@ -3474,7 +3566,7 @@ static public Var isMacro(Object op) throws Exception{ return null; } -static public IFn isInline(Object op) throws Exception{ +static public IFn isInline(Object op, int arity) throws Exception{ //no local inlines for now if(op instanceof Symbol && referenceLocal((Symbol) op) != null) return null; @@ -3485,7 +3577,13 @@ static public IFn isInline(Object op) throws Exception{ { if(v.ns != currentNS() && !v.isPublic()) throw new IllegalStateException("var: " + v + " is not public"); - return (IFn) RT.get(v.meta(), inlineKey); + IFn ret = (IFn) RT.get(v.meta(), inlineKey); + if(ret != null) + { + IPersistentSet arities = (IPersistentSet) RT.get(v.meta(), inlineAritiesKey); + if(arities == null || arities.contains(arity)) + return ret; + } } } return null; @@ -3562,7 +3660,7 @@ private static Expr analyzeSeq(C context, ISeq form, String name) throws Excepti return analyze(context, me, name); Object op = RT.first(form); - IFn inline = isInline(op); + IFn inline = isInline(op, RT.count(RT.rest(form))); if(inline != null) return analyze(context, inline.applyTo(RT.rest(form))); IParser p; diff --git a/src/jvm/clojure/lang/Numbers.java b/src/jvm/clojure/lang/Numbers.java index e833e693..6e469f31 100644 --- a/src/jvm/clojure/lang/Numbers.java +++ b/src/jvm/clojure/lang/Numbers.java @@ -986,54 +986,54 @@ final static class IntegerBitOps implements BitOps{ } public Number clearBit(Number x, int n){ - if (n < 31) + if(n < 31) return x.intValue() & ~(1 << n); - else if (n < 63) + else if(n < 63) return x.longValue() & ~(1L << n); else return toBigInteger(x).clearBit(n); } public Number setBit(Number x, int n){ - if (n < 31) + if(n < 31) return x.intValue() | (1 << n); - else if (n < 63) + else if(n < 63) return x.longValue() | (1L << n); else return toBigInteger(x).setBit(n); } public Number flipBit(Number x, int n){ - if (n < 31) + if(n < 31) return x.intValue() ^ (1 << n); - else if (n < 63) + else if(n < 63) return x.longValue() ^ (1L << n); else return toBigInteger(x).flipBit(n); } public boolean testBit(Number x, int n){ - if (n < 32) + if(n < 32) return (x.intValue() & (1 << n)) != 0; - else if (n < 64) + else if(n < 64) return (x.longValue() & (1L << n)) != 0; else return toBigInteger(x).testBit(n); } public Number shiftLeft(Number x, int n){ - if (n < 32) + if(n < 32) { - if (n < 0) + if(n < 0) return shiftRight(x, -n); return reduce(x.longValue() << n); } else return reduce(toBigInteger(x).shiftLeft(n)); } - + public Number shiftRight(Number x, int n){ - if (n < 0) + if(n < 0) return shiftLeft(x, -n); return x.intValue() >> n; } @@ -1077,41 +1077,41 @@ final static class LongBitOps implements BitOps{ } public Number clearBit(Number x, int n){ - if (n < 63) + if(n < 63) return x.longValue() & ~(1L << n); else return toBigInteger(x).clearBit(n); } public Number setBit(Number x, int n){ - if (n < 63) + if(n < 63) return x.longValue() | (1L << n); else return toBigInteger(x).setBit(n); } public Number flipBit(Number x, int n){ - if (n < 63) + if(n < 63) return x.longValue() ^ (1L << n); else return toBigInteger(x).flipBit(n); } public boolean testBit(Number x, int n){ - if (n < 64) + if(n < 64) return (x.longValue() & (1L << n)) != 0; else return toBigInteger(x).testBit(n); } public Number shiftLeft(Number x, int n){ - if (n < 0) + if(n < 0) return shiftRight(x, -n); return reduce(toBigInteger(x).shiftLeft(n)); } public Number shiftRight(Number x, int n){ - if (n < 0) + if(n < 0) return shiftLeft(x, -n); return x.longValue() >> n; } @@ -1226,7 +1226,6 @@ static BitOps bitOps(Object x){ return INTEGER_BITOPS; } - //final static ExecutorService executor = Executors.newCachedThreadPool(); //static public int minChunk = 100; //static int chunkSize(int alength){ @@ -1251,41 +1250,322 @@ static BitOps bitOps(Object x){ // executor.invokeAll(ops); // } -static final public class Indexer{ - int i; +static public Number num(float x){ + return x; +} + +static public float prim_add(float x, float y){ + return x + y; +} + +static public float prim_subtract(float x, float y){ + return x - y; +} + +static public float prim_negate(float x){ + return -x; +} + +static public float prim_inc(float x){ + return x + 1; +} + +static public float prim_dec(float x){ + return x - 1; +} + +static public float prim_multiply(float x, float y){ + return x * y; +} + +static public float prim_divide(float x, float y){ + return x / y; +} + +static public boolean prim_equiv(float x, float y){ + return x == y; +} + +static public boolean prim_lt(float x, float y){ + return x < y; +} + +static public boolean prim_lte(float x, float y){ + return x <= y; +} + +static public boolean prim_gt(float x, float y){ + return x > y; +} + +static public boolean prim_gte(float x, float y){ + return x >= y; +} + +static public boolean prim_isPos(float x){ + return x > 0; +} + +static public boolean prim_isNeg(float x){ + return x < 0; +} + +static public boolean prim_isZero(float x){ + return x == 0; +} + +static public Number num(double x){ + return x; +} + +static public double prim_add(double x, double y){ + return x + y; +} + +static public double prim_subtract(double x, double y){ + return x - y; +} + +static public double prim_negate(double x){ + return -x; +} + +static public double prim_inc(double x){ + return x + 1; +} + +static public double prim_dec(double x){ + return x - 1; +} + +static public double prim_multiply(double x, double y){ + return x * y; +} + +static public double prim_divide(double x, double y){ + return x / y; +} + +static public boolean prim_equiv(double x, double y){ + return x == y; +} + +static public boolean prim_lt(double x, double y){ + return x < y; +} + +static public boolean prim_lte(double x, double y){ + return x <= y; +} + +static public boolean prim_gt(double x, double y){ + return x > y; +} + +static public boolean prim_gte(double x, double y){ + return x >= y; +} + +static public boolean prim_isPos(double x){ + return x > 0; +} + +static public boolean prim_isNeg(double x){ + return x < 0; +} + +static public boolean prim_isZero(double x){ + return x == 0; +} + +static public Number num(int x){ + return x; +} + +static public int prim_add(int x, int y){ + return x + y; +} + +static public int prim_subtract(int x, int y){ + return x - y; +} + +static public int prim_negate(int x){ + return -x; +} + +static public int prim_inc(int x){ + return x + 1; +} + +static public int prim_dec(int x){ + return x - 1; +} + +static public int prim_multiply(int x, int y){ + return x * y; +} + +static public int prim_divide(int x, int y){ + return x / y; +} + +static public boolean prim_equiv(int x, int y){ + return x == y; +} + +static public boolean prim_lt(int x, int y){ + return x < y; +} + +static public boolean prim_lte(int x, int y){ + return x <= y; +} - public Indexer(int i){ - this.i = i; +static public boolean prim_gt(int x, int y){ + return x > y; +} + +static public boolean prim_gte(int x, int y){ + return x >= y; +} + +static public boolean prim_isPos(int x){ + return x > 0; +} + +static public boolean prim_isNeg(int x){ + return x < 0; +} + +static public boolean prim_isZero(int x){ + return x == 0; +} + +static public Number num(long x){ + return x; +} + +static public long prim_add(long x, long y){ + return x + y; +} + +static public long prim_subtract(long x, long y){ + return x - y; +} + +static public long prim_negate(long x){ + return -x; +} + +static public long prim_inc(long x){ + return x + 1; +} + +static public long prim_dec(long x){ + return x - 1; +} + +static public long prim_multiply(long x, long y){ + return x * y; +} + +static public long prim_divide(long x, long y){ + return x / y; +} + +static public boolean prim_equiv(long x, long y){ + return x == y; +} + +static public boolean prim_lt(long x, long y){ + return x < y; +} + +static public boolean prim_lte(long x, long y){ + return x <= y; +} + +static public boolean prim_gt(long x, long y){ + return x > y; +} + +static public boolean prim_gte(long x, long y){ + return x >= y; +} + +static public boolean prim_isPos(long x){ + return x > 0; +} + +static public boolean prim_isNeg(long x){ + return x < 0; +} + +static public boolean prim_isZero(long x){ + return x == 0; +} + +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; } - public final int get(){ - return i; + static public float negate(float x){ + return -x; } - public final int inc(){ - return i += 1; + static public float inc(float x){ + return x + 1; } - public final int inc(int n){ - return i += n; + + static public float dec(float x){ + return x - 1; } -} -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 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 aget(float[] xs, int i){ return xs[i]; @@ -1308,29 +1588,29 @@ static public class F{ float[] ret = new float[size]; if(init instanceof Number) { - float f = ((Number)init).floatValue(); - for(int i=0;i<ret.length;i++) + 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(); + for(int i = 0; i < size && s != null; i++, s = s.rest()) + ret[i] = ((Number) s.first()).floatValue(); } return ret; } static public float[] vec(Object sizeOrSeq){ if(sizeOrSeq instanceof Number) - return new float[((Number)sizeOrSeq).intValue()]; + return new float[((Number) sizeOrSeq).intValue()]; else { ISeq s = RT.seq(sizeOrSeq); int size = s.count(); float[] ret = new float[size]; - for(int i = 0; i < size && s != null;i++,s=s.rest()) - ret[i] = ((Number)s.first()).intValue(); + for(int i = 0; i < size && s != null; i++, s = s.rest()) + ret[i] = ((Number) s.first()).intValue(); return ret; } } @@ -1338,339 +1618,383 @@ static public class F{ static public float[] vsadd(float[] x, float y){ final float[] xs = x.clone(); - for(int i=0;i<xs.length;i++) + 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++) + 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++) + 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++) + 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]; + 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]; + 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]; + 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; + 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; + 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++) + 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++) + 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++) + 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++) + 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++) + 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++) + for(int i = 0; i < xs.length; i++) { - if(xs[i]<low) + if(xs[i] < low) xs[i] = low; - else if(xs[i]>high) + 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++) + for(int i = 0; i < xs.length; i++) { - if(xs[i]<low) + if(xs[i] < low) { ++lowc; xs[i] = low; } - else if(xs[i]>high) + else if(xs[i] > high) { ++highc; xs[i] = high; } } - return RT.vector(xs,lowc,highc); - } + 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++) + for(int i = 0; i < xs.length; i++) { - if(xs[i]<thresh) + 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++) + 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++) + 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++) + 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]); + 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]); + for(int i = 0; i < xs.length; i++) + ret = Math.min(ret, xs[i]); return ret; - } + } - static public float vmean(float[] xs) { + static public float vmean(float[] xs){ if(xs.length == 0) return 0; - return vsum(xs)/xs.length; + return vsum(xs) / xs.length; } - static public double vrms(float[] xs) { + 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); + 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) { + static public float vsum(float[] xs){ float ret = 0; - for(int i=0;i<xs.length;i++) + 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) ; + 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++) + 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++) + 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]; + 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]; + 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; + for(int i = 0; i < xs.length; i++) + xs[i] = (xs[i] + ys[i]) * z; return xs; - } < |