From cea35eadb009352d8b58b021193e2ad77943f699 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Sat, 20 May 2023 20:12:43 +0800 Subject: [PATCH] made vector iterable --- .../main/java/net/woggioni/jmath/Matrix.java | 35 ++++++++++- .../java/net/woggioni/jmath/Rational.java | 7 ++- .../main/java/net/woggioni/jmath/Vector.java | 59 ++++++++++++++++++- 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/jmath/src/main/java/net/woggioni/jmath/Matrix.java b/jmath/src/main/java/net/woggioni/jmath/Matrix.java index dbdfccf..f803d3a 100644 --- a/jmath/src/main/java/net/woggioni/jmath/Matrix.java +++ b/jmath/src/main/java/net/woggioni/jmath/Matrix.java @@ -1,17 +1,19 @@ package net.woggioni.jmath; +import lombok.Data; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; import lombok.SneakyThrows; +import java.util.Iterator; import java.util.Objects; import java.util.function.BiFunction; import java.util.function.Function; import static net.woggioni.jwo.Requirement.require; -public class Matrix> { +public class Matrix> implements Iterable> { public interface Pivot { > Matrix mul(Matrix m); @@ -558,4 +560,35 @@ public class Matrix> { public T norm2() { return squaredNorm2().sqrt(); } + + @Override + public Iterator> iterator() { + return new Iterator<>() { + int i = 0; + int j = 0; + + @Override + public boolean hasNext() { + return i * getColumns() + j < getRows() * getColumns(); + } + + @Override + public Element next() { + Element result = new Element<>(i, j, get(i, j)); + ++j; + if (j == getColumns()) { + j = 0; + ++i; + } + return result; + } + }; + } + + @Data + public static class Element { + private final int row; + private final int column; + private final T value; + } } diff --git a/jmath/src/main/java/net/woggioni/jmath/Rational.java b/jmath/src/main/java/net/woggioni/jmath/Rational.java index a76c269..124b14e 100644 --- a/jmath/src/main/java/net/woggioni/jmath/Rational.java +++ b/jmath/src/main/java/net/woggioni/jmath/Rational.java @@ -68,7 +68,12 @@ public class Rational implements NumericType { public static Rational of(long n) { return new Rational(n, 1); } - + public static Rational of(BigInteger num) { + return new Rational(num, BigInteger.ONE); + } + public static Rational of(BigInteger num, BigInteger den) { + return new Rational(num, den); + } private Rational simplify() { BigInteger gcd = BigIntegerExt.gcd(num.abs(), den.abs()); num = num.divide(gcd); diff --git a/jmath/src/main/java/net/woggioni/jmath/Vector.java b/jmath/src/main/java/net/woggioni/jmath/Vector.java index 32045c2..8d1f1d1 100644 --- a/jmath/src/main/java/net/woggioni/jmath/Vector.java +++ b/jmath/src/main/java/net/woggioni/jmath/Vector.java @@ -1,15 +1,19 @@ package net.woggioni.jmath; import lombok.AccessLevel; +import lombok.Data; import lombok.RequiredArgsConstructor; +import java.util.Iterator; import java.util.Objects; +import java.util.function.BiFunction; +import java.util.function.Function; import java.util.function.IntFunction; import static net.woggioni.jwo.Requirement.require; @RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public class Vector> { +public class Vector> implements Iterable> { private final NumericTypeFactory numericTypeFactory; private final T[] values; @@ -39,6 +43,35 @@ public class Vector> { require(() -> other.size() == size()).otherwise(SizeException.class, "Vectors must be of same size"); } + public Vector map(Vector other, BiFunction op) { + requireSameSize(other); + return of(numericTypeFactory, size(), (i) -> op.apply(get(i), other.get(i))); + } + + public > Vector map(NumericTypeFactory numericTypeFactory, Function op) { + return of(numericTypeFactory, size(), (i) -> op.apply(get(i))); + } + + public Vector map(Function op) { + return of(numericTypeFactory, size(), (i) -> op.apply(get(i))); + } + + public Vector sum(T f) { + return new Vector<>(numericTypeFactory, size(), index -> get(index).add(f)); + } + + public Vector sub(T f) { + return new Vector<>(numericTypeFactory, size(), index -> get(index).sub(f)); + } + + public Vector div(T f) { + return new Vector<>(numericTypeFactory, size(), index -> get(index).div(f)); + } + + public Vector mul(T f) { + return new Vector<>(numericTypeFactory, size(), index -> get(index).mul(f)); + } + public Vector sum(Vector other) { requireSameSize(other); return new Vector<>(numericTypeFactory, size(), index -> get(index).add(other.get(index))); @@ -126,6 +159,24 @@ public class Vector> { return sb.toString(); } + @Override + public Iterator> iterator() { + return new Iterator>() { + private int i = 0; + @Override + public boolean hasNext() { + return i < size(); + } + + @Override + public Element next() { + Element result = new Element<>(i, get(i)); + i++; + return result; + } + }; + } + public static > Vector of(NumericTypeFactory numericTypeFactory, T... values) { return new Vector<>(numericTypeFactory, values); } @@ -139,4 +190,10 @@ public class Vector> { IntFunction generator) { return new Vector<>(numericTypeFactory, size, generator); } + + @Data + public static class Element { + private final int index; + private final T value; + } }