Skip to content

Commit

Permalink
fixed and moved some code
Browse files Browse the repository at this point in the history
  • Loading branch information
Michiel Meeuwissen committed Jan 3, 2025
1 parent 1483346 commit a90c436
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import org.meeuw.math.DigitUtils;
import org.meeuw.math.abstractalgebra.FieldElement;
import org.meeuw.math.abstractalgebra.padic.impl.AdicDigits;
import org.meeuw.math.abstractalgebra.padic.impl.Utils;
import org.meeuw.math.abstractalgebra.padic.impl.AdicDigitUtils;
import org.meeuw.math.exceptions.*;

import static org.meeuw.math.abstractalgebra.padic.impl.AdicDigits.NOT_REPETITIVE;
import static org.meeuw.math.abstractalgebra.padic.impl.Utils.*;
import static org.meeuw.math.abstractalgebra.padic.impl.AdicDigitUtils.*;

/**
* WIP
Expand Down Expand Up @@ -92,7 +92,7 @@ byte digit(int n, byte[] digits, byte[] repetitive) {

@Override
public PAdicInteger negation() {
return new PAdicInteger(structure, Utils.negate((byte) structure.base, digits));
return new PAdicInteger(structure, AdicDigitUtils.negate((byte) structure.base, digits));
}

public PAdicInteger leftShift(int i) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public String toString() {
@Override
public PAdicInteger nextRandom(Random r) {

int[] nonRepetitive = new int[(int) Math.abs(r.nextGaussian(0, 10))];
int[] nonRepetitive = new int[(int) Math.abs(r.nextGaussian() * 10d)];
for (int i = 0 ; i < nonRepetitive.length; i++) {
nonRepetitive[i] = r.nextInt(base);
}
int[] repetitive = new int[(int) Math.abs(r.nextGaussian(0, 10))];
int[] repetitive = new int[(int) Math.abs(r.nextGaussian() * 10d)];
for (int i = 0 ; i < repetitive.length; i++) {
repetitive[i] = r.nextInt(base);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import static java.lang.System.arraycopy;
import static org.meeuw.math.DigitUtils.multiplyInverseDigitsWithCarry;

public class Utils {
public class AdicDigitUtils {
/**
* Long multiplication of adic numbers. With proper propagation of the repetitive part.
* @param base The base to interpret the digitis for
Expand All @@ -31,7 +31,7 @@ public static AdicDigits multiplyAdicDigits(
}
if (multiplierDigit.value != 0) {

var shiftedMultiplicand = multiplicand.leftShift(i);
AdicDigits shiftedMultiplicand = multiplicand.leftShift(i);
final AdicDigits summand = multiplyAdicDigits(base,
multiplierDigit.value,
shiftedMultiplicand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@ public void simple() {
}

@Test
@Disabled
public void negationOfOne() {
PAdicInteger minusOne = p5.one().negation();
assertThat(minusOne.toString()).isEqualTo("...4 ₅");


// FAILS
assertThat(p5.one().plus(minusOne)).isEqualTo(p5.zero());
assertThat(minusOne.negation()).isEqualTo(p5.one());
}
Expand All @@ -58,7 +55,7 @@ public void negation() {
public void random() {
Random r = new Random(1);
PAdicInteger random = p5.nextRandom(r);
assertThat(random.toString()).isEqualTo("...33432 2344₅");
assertThat(random.toString()).isEqualTo("...120444 44133432324221₅");
for (int i = 0; i < 200; i++) {
log.info("{}", p5.nextRandom(r));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import org.junit.jupiter.api.Test;

import org.meeuw.math.abstractalgebra.padic.impl.AdicDigits;
import org.meeuw.math.abstractalgebra.padic.impl.Utils;
import org.meeuw.math.abstractalgebra.padic.impl.AdicDigitUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.meeuw.math.abstractalgebra.padic.impl.Utils.multiplyAdicDigits;
import static org.meeuw.math.abstractalgebra.padic.impl.AdicDigitUtils.multiplyAdicDigits;

@Log4j2

public class UtilsTest {
public class AdicDigitUtilsTest {


@Test
Expand All @@ -30,37 +30,36 @@ public void shift() {
}
@Test
public void shift2() {
assertThat(AdicDigits.of("2", "").leftShift(1)
.toString()).isEqualTo("...2 0");
assertThat(AdicDigits.of("2", "").leftShift(1).toString()).isEqualTo("...2 0");
}

@Test
public void multiplyAdicDigitsWithDigit() {
AdicDigits a1 = AdicDigits.of("9", "89");
AdicDigits product = multiplyAdicDigits(10, (byte) 9, a1);
assertThat(Utils.adicToString( 10, product)).isEqualTo("...9 01₁₀");
assertThat(AdicDigitUtils.adicToString( 10, product)).isEqualTo("...9 01₁₀");
}

@Test
public void multiplyAdicDigitsWithDigit2() {
AdicDigits a1 = AdicDigits.of("5", "4");
AdicDigits product = multiplyAdicDigits(10, (byte) 6, a1);
assertThat(Utils.adicToString( 10, product)).isEqualTo("...3 24₁₀");
assertThat(AdicDigitUtils.adicToString( 10, product)).isEqualTo("...3 24₁₀");
}

@Test
public void multiplyAdicDigitsWithDigit3() {
AdicDigits a1 = AdicDigits.of("5", "4");
AdicDigits product = multiplyAdicDigits(10, (byte) 7, a1);
assertThat(Utils.adicToString(10, product)).isEqualTo("...8 78₁₀");
assertThat(AdicDigitUtils.adicToString(10, product)).isEqualTo("...8 78₁₀");
}


@Test
public void multiplyAdicDigitsWithDigit4() {
AdicDigits a1 = AdicDigits.of("6", "");
AdicDigits product = multiplyAdicDigits(10, (byte) 4, a1);
assertThat(Utils.adicToString(10, product)).isEqualTo("...6 4₁₀");
assertThat(AdicDigitUtils.adicToString(10, product)).isEqualTo("...6 4₁₀");
}

@Test
Expand All @@ -74,70 +73,70 @@ public void multiplyAdicDigitsWithDigit5() {
public void multiplyAdicDigitsWithDigitNonRepetitive() {
AdicDigits a1 = AdicDigits.of("0", "5678");
AdicDigits sum = multiplyAdicDigits((byte) 10, (byte) 5, a1);
assertThat(Utils.adicToString((byte) 10, sum)).isEqualTo("...0 28390₁₀");
assertThat(AdicDigitUtils.adicToString((byte) 10, sum)).isEqualTo("...0 28390₁₀");
}

@Test
public void multiplyAdicDigitsWithDigitNonRepetitive2() {
AdicDigits a1 = AdicDigits.of("0", "56780000");
AdicDigits sum = multiplyAdicDigits(10, (byte) 1, a1);
assertThat(Utils.adicToString((byte) 10, sum)).isEqualTo("...0 56780000₁₀");
assertThat(AdicDigitUtils.adicToString((byte) 10, sum)).isEqualTo("...0 56780000₁₀");
}

@Test
public void sumAdicDigits() {
AdicDigits a1 = AdicDigits.create(new byte[] {1}, new byte[] {});
AdicDigits sum = Utils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(Utils.adicToString((byte) 10, sum)).isEqualTo("...3 ₁₀");
AdicDigits sum = AdicDigitUtils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(AdicDigitUtils.adicToString((byte) 10, sum)).isEqualTo("...3 ₁₀");
}

@Test
public void sumAdicDigits2() {
AdicDigits a1 = AdicDigits.create(new byte[] {1, 3}, new byte[] {});
AdicDigits sum = Utils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(Utils.adicToString((byte) 10, sum)).isEqualTo("...93 ₁₀");
AdicDigits sum = AdicDigitUtils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(AdicDigitUtils.adicToString((byte) 10, sum)).isEqualTo("...93 ₁₀");
}

@Test
public void sumAdicDigits3() {
AdicDigits a1 = AdicDigits.of("0", "13314910");
AdicDigits a2 = AdicDigits.of("0", "56780000");
AdicDigits sum = Utils.sumAdicDigits( 10, a1, a1, a1);
assertThat(Utils.adicToString(10, sum)).isEqualTo("...0 39944730₁₀");
AdicDigits sum = AdicDigitUtils.sumAdicDigits( 10, a1, a1, a1);
assertThat(AdicDigitUtils.adicToString(10, sum)).isEqualTo("...0 39944730₁₀");
}


@Test
public void sumAdicDigitsWithOverflow() {
AdicDigits a1 = AdicDigits.create(new byte[] {1, 4}, new byte[] {});
AdicDigits sum = Utils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(Utils.adicToString((byte) 10, sum)).isEqualTo("...42 3₁₀");
AdicDigits sum = AdicDigitUtils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(AdicDigitUtils.adicToString((byte) 10, sum)).isEqualTo("...42 3₁₀");
}


@Test
public void sumAdicDigitsWithOverflowAndDigits() {
AdicDigits a1 = AdicDigits.create(new byte[] {1, 4}, new byte[] {1, 2, 3, 4});
AdicDigits sum = Utils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(Utils.adicToString((byte) 10, sum)).isEqualTo("...42 963₁₀");
AdicDigits sum = AdicDigitUtils.sumAdicDigits((byte) 10, a1, a1, a1);
assertThat(AdicDigitUtils.adicToString((byte) 10, sum)).isEqualTo("...42 963₁₀");
}

@Test
public void sumAdicDigitsWithOverflowAndDigits2() {
AdicDigits a1 = AdicDigits.ofRepetitive( 1, 2, 3).withDigits(1);
AdicDigits a2 = AdicDigits.ofRepetitive(7, 1).withDigits( 3, 4);

AdicDigits sum = Utils.sumAdicDigits(10, a1, a2);
assertThat(Utils.adicToString(10, sum)).isEqualTo("...402948 365₁₀");
AdicDigits sum = AdicDigitUtils.sumAdicDigits(10, a1, a2);
assertThat(AdicDigitUtils.adicToString(10, sum)).isEqualTo("...402948 365₁₀");
}

@Test
public void sumAdicDigitsWithOverflowAndDigits3() {
AdicDigits a1 = AdicDigits.ofRepetitive( 6, 7).withDigits(8, 9);
AdicDigits a2 = AdicDigits.ofRepetitive(5).withDigits( 1, 6, 6, 6);

AdicDigits sum = Utils.sumAdicDigits(10, a1, a2);
assertThat(Utils.adicToString(10, sum)).isEqualTo("...32 28455₁₀");
AdicDigits sum = AdicDigitUtils.sumAdicDigits(10, a1, a2);
assertThat(AdicDigitUtils.adicToString(10, sum)).isEqualTo("...32 28455₁₀");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.meeuw.test.math.abstractalgebra.padic.impl;

import org.junit.jupiter.api.Test;

import org.meeuw.math.abstractalgebra.padic.impl.AdicDigits;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class AdicDigitsTest {

@Test
void repeating() {
AdicDigits a = AdicDigits.of(3);
assertThat(a.repeating(0)).isFalse();
assertThat(a.repeating(1)).isFalse();
}
}

0 comments on commit a90c436

Please sign in to comment.