-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
448 additions
and
0 deletions.
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...algebra/src/test/java/org/meeuw/test/math/abstractalgebra/dim2/dim3/FieldMatrix2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2022 Michiel Meeuwissen | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.meeuw.test.math.abstractalgebra.dim2.dim3; | ||
|
||
import lombok.extern.log4j.Log4j2; | ||
|
||
import net.jqwik.api.*; | ||
import org.junit.jupiter.api.Test; | ||
import org.assertj.core.api.Assertions; | ||
|
||
import org.meeuw.math.abstractalgebra.dim2.FieldMatrix2; | ||
import org.meeuw.math.abstractalgebra.dim2.FieldMatrix2Group; | ||
import org.meeuw.math.abstractalgebra.dim3.FieldMatrix3; | ||
import org.meeuw.math.abstractalgebra.rationalnumbers.RationalNumber; | ||
import org.meeuw.math.abstractalgebra.rationalnumbers.RationalNumbers; | ||
import org.meeuw.math.exceptions.InvalidElementCreationException; | ||
import org.meeuw.math.exceptions.ReciprocalException; | ||
import org.meeuw.theories.abstractalgebra.MultiplicativeGroupTheory; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.meeuw.math.abstractalgebra.rationalnumbers.RationalNumber.of; | ||
|
||
/** | ||
* @author Michiel Meeuwissen | ||
* @since 0.14 | ||
*/ | ||
@PropertyDefaults(tries = 100) | ||
@Log4j2 | ||
class FieldMatrix2Test implements MultiplicativeGroupTheory<FieldMatrix2<RationalNumber>> { | ||
|
||
|
||
@Test | ||
public void illegal() { | ||
Assertions.assertThatThrownBy(() -> { | ||
FieldMatrix2.of( | ||
of(3), of(0), | ||
of(2), of(0) | ||
); | ||
}).isInstanceOf(InvalidElementCreationException.class); | ||
|
||
FieldMatrix3<RationalNumber> iml = new FieldMatrix3<>( | ||
new RationalNumber[][]{ | ||
new RationalNumber[]{of(3), of(0)}, | ||
new RationalNumber[]{of(2), of(0)} | ||
} | ||
); | ||
Assertions.assertThatThrownBy(iml::reciprocal) | ||
.isInstanceOf(ReciprocalException.class); | ||
|
||
} | ||
|
||
@Test | ||
public void adjugate() { | ||
FieldMatrix2<RationalNumber> fm = FieldMatrix2.of( | ||
of(3), of(0), | ||
of(2), of(0) | ||
|
||
); | ||
assertThat(fm.adjugate()).isEqualTo( | ||
FieldMatrix3.of( | ||
of(2), of(2), of(0), | ||
of(-2), of(3), of(10), | ||
of(2), of(-3), of(0) | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
public void determinant() { | ||
FieldMatrix2<RationalNumber> fm = FieldMatrix2.of( | ||
of(2), of(-3), | ||
of(2), of(0) | ||
); | ||
RationalNumber determinant = fm.determinant(); | ||
assertThat(determinant).isEqualTo(of(49)); | ||
log.info("Determinant {}", determinant); | ||
} | ||
|
||
@Test | ||
public void illegalReciprocal() { | ||
|
||
FieldMatrix3<RationalNumber> fm = new FieldMatrix3<>( | ||
new RationalNumber[][]{ | ||
new RationalNumber[]{of(2), of(0), of(-1)}, | ||
new RationalNumber[]{of(2), of(0), of(-1)}, | ||
new RationalNumber[]{of(1), of(4), of(5)} | ||
} | ||
); | ||
|
||
assertThatThrownBy(fm::reciprocal).isInstanceOf(ReciprocalException.class); | ||
} | ||
|
||
@Test | ||
public void determinantOfIdentity() { | ||
Assertions.assertThat(FieldMatrix2Group.of(RationalNumbers.INSTANCE).one() | ||
.determinant()).isEqualTo(of(1)); | ||
} | ||
|
||
@Override | ||
public Arbitrary<FieldMatrix2<RationalNumber>> elements() { | ||
return | ||
Arbitraries.randoms().map(RationalNumbers.INSTANCE::nextRandom).list().ofSize(4) | ||
.map( l -> FieldMatrix2.of( | ||
l.get(0), l.get(1), | ||
l.get(2), l.get(3) | ||
) | ||
) | ||
; | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...algebra/src/test/java/org/meeuw/test/math/abstractalgebra/dim2/dim3/FieldVector2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2022 Michiel Meeuwissen | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.meeuw.test.math.abstractalgebra.dim2.dim3; | ||
|
||
import net.jqwik.api.Arbitraries; | ||
import net.jqwik.api.Arbitrary; | ||
import org.junit.jupiter.api.Test; | ||
import org.assertj.core.api.Assertions; | ||
|
||
import org.meeuw.math.abstractalgebra.dim2.FieldVector2; | ||
import org.meeuw.math.abstractalgebra.dim2.FieldVector2Space; | ||
import org.meeuw.math.abstractalgebra.rationalnumbers.RationalNumber; | ||
import org.meeuw.math.abstractalgebra.rationalnumbers.RationalNumbers; | ||
import org.meeuw.math.abstractalgebra.reals.*; | ||
import org.meeuw.math.exceptions.FieldIncompleteException; | ||
import org.meeuw.theories.abstractalgebra.*; | ||
|
||
import static java.math.BigDecimal.valueOf; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.meeuw.math.abstractalgebra.reals.BigDecimalElement.of; | ||
import static org.meeuw.math.uncertainnumbers.CompareConfiguration.withLooseEquals; | ||
|
||
/** | ||
* @author Michiel Meeuwissen | ||
* @since 0.4 | ||
*/ | ||
class FieldVector2Test implements | ||
WithScalarTheory<FieldVector2<RealNumber>, RealNumber>, | ||
VectorSpaceTheory<FieldVector2<RealNumber>, RealNumber>, | ||
ElementTheory<FieldVector2<RealNumber>> { | ||
|
||
@Test | ||
public void abs() { | ||
FieldVector2<RealNumber> v = FieldVector2.of(3, -4); | ||
assertThat(v.abs()).isEqualTo(new RealNumber(5, 0)); | ||
} | ||
|
||
@Test | ||
public void absOfRational() { | ||
FieldVector2<RationalNumber> v = FieldVector2.of(RationalNumber.of(3), RationalNumber.of(-4)); | ||
assertThatThrownBy(v::abs).isInstanceOf(FieldIncompleteException.class); | ||
} | ||
|
||
@Test | ||
public void times() { | ||
FieldVector2<RationalNumber> v = FieldVector2.of(RationalNumber.of(3), RationalNumber.of(-4)); | ||
assertThat(v.times(RationalNumber.of(3, 2))).isEqualTo(FieldVector2.of(RationalNumber.of(9, 2), RationalNumber.of(-6))); | ||
} | ||
|
||
@Test | ||
public void dividedBy() { | ||
withLooseEquals(() -> { | ||
FieldVector2<BigDecimalElement> v = FieldVector2.of(valueOf(3), valueOf(-4)); | ||
assertThat(v.dividedBy(of(-2))).isEqualTo(FieldVector2.of(of(-1.5), of(2))); | ||
}); | ||
} | ||
|
||
@Test | ||
public void string() { | ||
FieldVector2<BigDecimalElement> v = FieldVector2.of(of(3), of(-4)); | ||
assertThat(v.toString()).isEqualTo("(3.0,-4.0)"); | ||
} | ||
|
||
@Test | ||
public void testEquals() { | ||
FieldVector2<BigDecimalElement> v1 = FieldVector2.of(of(3), of(-4)); | ||
FieldVector2<BigDecimalElement> v2 = FieldVector2.of(of(3), of(-4)); | ||
assertThat(v1).isEqualTo(v2); | ||
v2 = v2.withY(of(-3)); | ||
assertThat(v1).isNotEqualTo(v2); | ||
v2 = v2.withX(of(3)); | ||
assertThat(v1).isEqualTo(v2); | ||
} | ||
|
||
@SuppressWarnings({"ConstantConditions", "EqualsBetweenInconvertibleTypes"}) | ||
@Test | ||
public void spaceEquals() { | ||
Assertions.assertThat(new FieldVector2Space<>(RealField.INSTANCE).equals(new FieldVector2Space<>(RealField.INSTANCE))).isTrue(); | ||
assertThat(new FieldVector2Space<>(RealField.INSTANCE).hashCode()).isEqualTo(new FieldVector2Space<>(RealField.INSTANCE).hashCode()); | ||
assertThat(new FieldVector2Space<>(RealField.INSTANCE).equals(null)).isFalse(); | ||
assertThat(new FieldVector2Space<>(RationalNumbers.INSTANCE).equals(new FieldVector2Space<>(RealField.INSTANCE))).isFalse(); | ||
} | ||
|
||
|
||
@Override | ||
public Arbitrary<RealNumber> scalars() { | ||
return Arbitraries.randomValue(r -> | ||
new RealNumber(r.nextDouble() * 10, Math.abs(r.nextDouble())) | ||
) | ||
.injectDuplicates(0.1) | ||
.dontShrink() | ||
.edgeCases((config) -> { | ||
config.add(RealNumber.ZERO); | ||
config.add(RealNumber.ONE); | ||
}); | ||
} | ||
|
||
@Override | ||
public Arbitrary<? extends FieldVector2<RealNumber>> elements() { | ||
return Arbitraries.doubles() | ||
.between(-100, 100) | ||
.tuple2() | ||
.map((t) -> { | ||
return FieldVector2.of(t.get1(), t.get2()); | ||
}) | ||
.injectDuplicates(0.5) | ||
; | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
mihxil-algebra/src/test/java/org/meeuw/test/math/abstractalgebra/dim2/dim3/Matrix2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright 2022 Michiel Meeuwissen | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.meeuw.test.math.abstractalgebra.dim2.dim3; | ||
|
||
import java.math.*; | ||
|
||
import net.jqwik.api.Arbitraries; | ||
import net.jqwik.api.Arbitrary; | ||
import net.jqwik.api.lifecycle.AfterProperty; | ||
import net.jqwik.api.lifecycle.BeforeProperty; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.meeuw.math.abstractalgebra.dim2.Matrix2; | ||
import org.meeuw.math.abstractalgebra.dim2.Matrix2Group; | ||
|
||
import org.meeuw.math.abstractalgebra.reals.RealField; | ||
import org.meeuw.math.abstractalgebra.reals.RealNumber; | ||
import org.meeuw.theories.abstractalgebra.MultiplicativeGroupTheory; | ||
import org.meeuw.theories.abstractalgebra.WithScalarTheory; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.meeuw.math.abstractalgebra.dim2.Matrix2.of; | ||
|
||
/** | ||
* @author Michiel Meeuwissen | ||
*/ | ||
class Matrix2Test implements MultiplicativeGroupTheory<Matrix2>, WithScalarTheory<Matrix2, RealNumber> { | ||
|
||
|
||
@BeforeProperty | ||
public void approx() { | ||
Matrix2Group.INSTANCE.setDoubleEquivalence((v1, v2) -> round(v1) == round(v2)); | ||
} | ||
@AfterProperty | ||
public void shutdown() { | ||
Matrix2Group.INSTANCE.clearDoubleEquivalence(); | ||
} | ||
public static double round(double v) { | ||
if (Double.isNaN(v)){ | ||
return v; | ||
} | ||
return BigDecimal.valueOf(v).round(new MathContext(2)).setScale(0, RoundingMode.HALF_UP).doubleValue(); | ||
} | ||
|
||
@Override | ||
public Arbitrary<RealNumber> scalars() { | ||
return Arbitraries.randomValue((random) -> | ||
new RealNumber(random.nextDouble() * 200 - 100, random.nextDouble() * 10)) | ||
.dontShrink() | ||
.edgeCases(config -> { | ||
config.add(RealField.INSTANCE.zero()); | ||
config.add(RealField.INSTANCE.one()); | ||
config.add(RealField.INSTANCE.one().times(-1)); | ||
}); | ||
} | ||
|
||
@Test | ||
void times() { | ||
Matrix2 example = of( | ||
1, 2, | ||
4, 5 | ||
); | ||
assertThat(example.times(2).getValues()).isEqualTo( | ||
new double[][] { | ||
new double[] {2, 4}, | ||
new double[] {8, 10} | ||
} | ||
); | ||
assertThat(example.times(example.getStructure().one())) | ||
.isEqualTo(example); | ||
} | ||
|
||
|
||
|
||
@Override | ||
public Arbitrary<Matrix2> elements() { | ||
return Arbitraries.of( | ||
of( | ||
1, 2, | ||
4, 5 | ||
), | ||
of( | ||
4, 2.2, | ||
0, 1 | ||
) | ||
|
||
); | ||
} | ||
} |
Oops, something went wrong.