made vector iterable

This commit is contained in:
2023-05-20 20:12:43 +08:00
parent 9b09c6bd73
commit cea35eadb0
3 changed files with 98 additions and 3 deletions

View File

@@ -1,17 +1,19 @@
package net.woggioni.jmath; package net.woggioni.jmath;
import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import java.util.Iterator;
import java.util.Objects; import java.util.Objects;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import static net.woggioni.jwo.Requirement.require; import static net.woggioni.jwo.Requirement.require;
public class Matrix<T extends NumericType<T>> { public class Matrix<T extends NumericType<T>> implements Iterable<Matrix.Element<T>> {
public interface Pivot { public interface Pivot {
<U extends NumericType<U>> Matrix<U> mul(Matrix<U> m); <U extends NumericType<U>> Matrix<U> mul(Matrix<U> m);
@@ -558,4 +560,35 @@ public class Matrix<T extends NumericType<T>> {
public T norm2() { public T norm2() {
return squaredNorm2().sqrt(); return squaredNorm2().sqrt();
} }
@Override
public Iterator<Element<T>> iterator() {
return new Iterator<>() {
int i = 0;
int j = 0;
@Override
public boolean hasNext() {
return i * getColumns() + j < getRows() * getColumns();
}
@Override
public Element<T> next() {
Element<T> result = new Element<>(i, j, get(i, j));
++j;
if (j == getColumns()) {
j = 0;
++i;
}
return result;
}
};
}
@Data
public static class Element<T> {
private final int row;
private final int column;
private final T value;
}
} }

View File

@@ -68,7 +68,12 @@ public class Rational implements NumericType<Rational> {
public static Rational of(long n) { public static Rational of(long n) {
return new Rational(n, 1); 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() { private Rational simplify() {
BigInteger gcd = BigIntegerExt.gcd(num.abs(), den.abs()); BigInteger gcd = BigIntegerExt.gcd(num.abs(), den.abs());
num = num.divide(gcd); num = num.divide(gcd);

View File

@@ -1,15 +1,19 @@
package net.woggioni.jmath; package net.woggioni.jmath;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Data;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import java.util.Iterator;
import java.util.Objects; import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntFunction; import java.util.function.IntFunction;
import static net.woggioni.jwo.Requirement.require; import static net.woggioni.jwo.Requirement.require;
@RequiredArgsConstructor(access = AccessLevel.PRIVATE) @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class Vector<T extends NumericType<T>> { public class Vector<T extends NumericType<T>> implements Iterable<Vector.Element<T>> {
private final NumericTypeFactory<T> numericTypeFactory; private final NumericTypeFactory<T> numericTypeFactory;
private final T[] values; private final T[] values;
@@ -39,6 +43,35 @@ public class Vector<T extends NumericType<T>> {
require(() -> other.size() == size()).otherwise(SizeException.class, "Vectors must be of same size"); require(() -> other.size() == size()).otherwise(SizeException.class, "Vectors must be of same size");
} }
public Vector<T> map(Vector<T> other, BiFunction<T, T, T> op) {
requireSameSize(other);
return of(numericTypeFactory, size(), (i) -> op.apply(get(i), other.get(i)));
}
public <U extends NumericType<U>> Vector<U> map(NumericTypeFactory<U> numericTypeFactory, Function<T, U> op) {
return of(numericTypeFactory, size(), (i) -> op.apply(get(i)));
}
public Vector<T> map(Function<T, T> op) {
return of(numericTypeFactory, size(), (i) -> op.apply(get(i)));
}
public Vector<T> sum(T f) {
return new Vector<>(numericTypeFactory, size(), index -> get(index).add(f));
}
public Vector<T> sub(T f) {
return new Vector<>(numericTypeFactory, size(), index -> get(index).sub(f));
}
public Vector<T> div(T f) {
return new Vector<>(numericTypeFactory, size(), index -> get(index).div(f));
}
public Vector<T> mul(T f) {
return new Vector<>(numericTypeFactory, size(), index -> get(index).mul(f));
}
public Vector<T> sum(Vector<T> other) { public Vector<T> sum(Vector<T> other) {
requireSameSize(other); requireSameSize(other);
return new Vector<>(numericTypeFactory, size(), index -> get(index).add(other.get(index))); return new Vector<>(numericTypeFactory, size(), index -> get(index).add(other.get(index)));
@@ -126,6 +159,24 @@ public class Vector<T extends NumericType<T>> {
return sb.toString(); return sb.toString();
} }
@Override
public Iterator<Element<T>> iterator() {
return new Iterator<Element<T>>() {
private int i = 0;
@Override
public boolean hasNext() {
return i < size();
}
@Override
public Element<T> next() {
Element<T> result = new Element<>(i, get(i));
i++;
return result;
}
};
}
public static <T extends NumericType<T>> Vector<T> of(NumericTypeFactory<T> numericTypeFactory, T... values) { public static <T extends NumericType<T>> Vector<T> of(NumericTypeFactory<T> numericTypeFactory, T... values) {
return new Vector<>(numericTypeFactory, values); return new Vector<>(numericTypeFactory, values);
} }
@@ -139,4 +190,10 @@ public class Vector<T extends NumericType<T>> {
IntFunction<T> generator) { IntFunction<T> generator) {
return new Vector<>(numericTypeFactory, size, generator); return new Vector<>(numericTypeFactory, size, generator);
} }
@Data
public static class Element<T> {
private final int index;
private final T value;
}
} }