Skip to content

Commit

Permalink
Fixing test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Nov 9, 2024
1 parent 701034d commit 44c7e27
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import java.math.BigInteger;

import java.util.Arrays;

import org.meeuw.math.ArrayUtils;
import org.meeuw.math.DigitUtils;
import org.meeuw.math.DigitUtils.AdicDigits;
import org.meeuw.math.abstractalgebra.*;
import org.meeuw.math.abstractalgebra.FieldElement;
import org.meeuw.math.exceptions.*;

import static org.meeuw.math.DigitUtils.AdicDigits.NOT_REPETITIVE;
Expand All @@ -23,7 +22,7 @@ public class PAdicInteger implements FieldElement<PAdicInteger> {
this.digits = digits;
this.structure = structure;
for (byte i : digits.digits) {
if (Byte.compareUnsigned(i, structure.base) > 0) {
if (Byte.toUnsignedInt(i) > structure.base) {
throw new InvalidElementCreationException("digit must be smaller than " + structure.base);
}
}
Expand All @@ -34,7 +33,7 @@ public class PAdicInteger implements FieldElement<PAdicInteger> {


public BigInteger bigIntegerValue() {
if (Arrays.compare(digits.repetitive , NOT_REPETITIVE) != 0) {
if (! ArrayUtils.equals(digits.repetitive , NOT_REPETITIVE)) {
throw new NotFiniteException(this + " is not finite");
}
BigInteger result = BigInteger.ZERO;
Expand All @@ -60,7 +59,7 @@ public PAdicInteger dividedBy(long divisor) {
@Override
public PAdicInteger times(long multiplier) {
AdicDigits mult = new AdicDigits(DigitUtils.toBase(structure.base, multiplier), new byte[0]);
return new PAdicInteger(structure, DigitUtils.multiplyPAdicDigits(structure.base, this.digits, mult));
return new PAdicInteger(structure, DigitUtils.multiplyPAdicDigits((byte) structure.base, this.digits, mult));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import org.meeuw.math.validation.Prime;

public class PAdicIntegers implements Field<PAdicInteger> {
final byte base;
final int base;
// could be byte, but that give a lot of casting, and stuff with toUnsignedInt
// there are only a few instances of this class, memory usage is no issue.

final BigInteger bbase;

private static final Map<Byte, PAdicIntegers> CACHE = new ConcurrentHashMap<>();

private PAdicIntegers(@Prime byte base) {
private PAdicIntegers(@Prime int base) {
this.base = base;
this.bbase = BigInteger.valueOf(base);
if (!IntegerUtils.isPrime(this.base)) {
Expand Down
18 changes: 16 additions & 2 deletions mihxil-math/src/main/java/org/meeuw/math/ArrayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,24 @@ public static <E> String toString(E[][] matrix) {
return toString(matrix, ArrayUtils::toString);
}

public
static double determinant2x2(double a, double b, double c, double d) {
public static double determinant2x2(double a, double b, double c, double d) {
return a * d - b * c;
}

public static boolean equals(byte[] a, byte[] b) {
if (a == b) {
return true;
}
if (a.length != b.length) {
return false;
}
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}


}
30 changes: 13 additions & 17 deletions mihxil-math/src/main/java/org/meeuw/math/DigitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public class DigitUtils {
private DigitUtils() {}


public static byte[] toBase(byte base, long value) {
public static byte[] toBase(int basei, long value) {
byte[] result = new byte[100];
int i = 0;
int basei = toUnsignedInt(base);
while(value > 0) {
result = ensureCapacity(i, result);
result[i] = (byte) (value % basei);
Expand Down Expand Up @@ -81,9 +80,8 @@ public static long fromInverseDigitsInBase(final byte base, @Min(0) byte... digi
}


public static byte[] multiplyInverseDigits(byte base, byte digit, byte[] multiplicand) {
public static byte[] multiplyInverseDigits(int basei, byte digit, byte[] multiplicand) {
byte[] result = new byte[multiplicand.length + 1];
int basei = toUnsignedInt(base);
int carry = 0;
for (int i = 0 ; i < multiplicand.length;i ++) {
int ri = carry ;
Expand All @@ -95,9 +93,8 @@ public static byte[] multiplyInverseDigits(byte base, byte digit, byte[] multipl
return result;
}

public static AdicDigits multiplyAdicDigits(byte base, byte digit, AdicDigits multiplicand) {
public static AdicDigits multiplyAdicDigits(int base, byte digit, AdicDigits multiplicand) {
byte[] resultdigits = multiplyInverseDigits(base, digit, multiplicand.digits);
int basei = toUnsignedInt(base);
int carry = toUnsignedInt(resultdigits[resultdigits.length - 1]);
int digiti = toUnsignedInt(digit);
List<CarryAndIndex> carries = new ArrayList<>();
Expand All @@ -110,8 +107,8 @@ public static AdicDigits multiplyAdicDigits(byte base, byte digit, AdicDigits mu
if (indexOf == -1) {
carries.add(carryAndIndex);
int ri = carry + digiti * toUnsignedInt(multiplicand.repetitive[index]);
moreDigits.add((byte) (ri % basei));
carry = ri / basei;
moreDigits.add((byte) (ri % base));
carry = ri / base;
i++;
} else {
break;
Expand All @@ -131,7 +128,7 @@ public static AdicDigits multiplyAdicDigits(byte base, byte digit, AdicDigits mu
}


public static AdicDigits multiplyPAdicDigits(@Prime byte base, AdicDigits multiplicator, AdicDigits multiplicand) {
public static AdicDigits multiplyPAdicDigits(@Prime int base, AdicDigits multiplicator, AdicDigits multiplicand) {
//assert IntegerUtils.isPrime(toUnsignedInt(base));
int i = 0;
AdicDigits sum = null;
Expand All @@ -156,10 +153,10 @@ public static AdicDigits multiplyPAdicDigits(@Prime byte base, AdicDigits multip
/**
* Performs 'long multiplication' on two numbers represented by a {@code byte[]}, where the least significant digit is the one at {@code [0]}
* @param base The base of this number
* @see #multiplyInverseDigits(byte, byte, byte[])
* @see #sumInverseDigits(byte, byte[]...)
* @see #multiplyInverseDigits(int, byte, byte[])
* @see #sumInverseDigits(int, byte[]...)
*/
public static byte[] multiplyInverseDigits(byte base, byte[] multiplicand1, byte[] multiplicand2) {
public static byte[] multiplyInverseDigits(int base, byte[] multiplicand1, byte[] multiplicand2) {

// swap if necessary.
if (multiplicand2.length < multiplicand1.length) {
Expand All @@ -182,12 +179,11 @@ public static byte[] multiplyInverseDigits(byte base, byte[] multiplicand1, byte

/**
* Performs a sum of number of integers
* @param base The base of this number
* @param basei The base of this number
*/
public static byte[] sumInverseDigits(byte base, byte[]... a) {
public static byte[] sumInverseDigits(int basei, byte[]... a) {
int max = maxLength(a);
int carry = 0;
int basei = Byte.toUnsignedInt(base);
byte[] result = new byte[max];
for (int i = 0 ; i < max;i ++) {
int ri = carry ;
Expand All @@ -201,8 +197,8 @@ public static byte[] sumInverseDigits(byte base, byte[]... a) {
byte[] newResult = new byte[result.length + 1];
arraycopy(result, 0, newResult, 0, result.length);
result = newResult;
result[result.length - 1] = (byte) (carry % base);
carry /= base;
result[result.length - 1] = (byte) (carry % basei);
carry /= basei;
}
return result;
}
Expand Down

0 comments on commit 44c7e27

Please sign in to comment.