Skip to content

Commit

Permalink
Experimented a bit with tetration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Oct 24, 2024
1 parent dfeaa35 commit 434c655
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean isZero() {
}

@Override
@NonAlgebraic
@NonAlgebraic(value="Factorials are never odd")
public IntegerElement factorial() {
return new IntegerElement(bigIntegerFactorial());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Quaternion<E> dividedBy(Quaternion<E> divisor) throws DivisionByZeroExcep
}

@Override
@NonAlgebraic
@NonAlgebraic(reason = NonAlgebraic.Reason.SOME)
public Quaternion<E> dividedBy(E divisor) {
return new Quaternion<>(
a.dividedBy(divisor),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import org.meeuw.math.abstractalgebra.*;
import org.meeuw.math.abstractalgebra.complex.ComplexNumber;
import org.meeuw.math.exceptions.*;
import static org.meeuw.math.operators.BasicAlgebraicBinaryOperator.POWER;
import org.meeuw.math.text.FormatService;
import org.meeuw.math.uncertainnumbers.*;

import static java.lang.Math.max;
import static org.meeuw.math.DoubleUtils.uncertaintyForDouble;
import static org.meeuw.math.operators.BasicAlgebraicBinaryOperator.POWER;
import static org.meeuw.math.text.TextUtils.superscript;

/**
Expand Down Expand Up @@ -240,7 +240,7 @@ public RealNumber sqrt() {
}

@Override
@NonAlgebraic
@NonAlgebraic(reason = NonAlgebraic.Reason.SOME)
public RealNumber pow(RealNumber exponent) throws IllegalPowerException, OverflowException {
if (value == 0 && exponent.isNegative()) {
throw new IllegalPowerException("0 ^ " + exponent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.meeuw.math.abstractalgebra.reals.BigDecimalElement.of;
import static org.meeuw.math.uncertainnumbers.CompareConfiguration.withLooseEquals;

/**
* @author Michiel Meeuwissen
Expand All @@ -48,6 +49,21 @@ public void testOf() {
assertThat(BigComplexNumber.of(BigDecimalElement.ONE)).isEqualTo(BigComplexNumber.of(BigDecimalElement.ONE, BigDecimalElement.ZERO));
}


@Test
public void tetration() {
BigComplexNumber i = BigComplexNumber.of(of(0), of(1));

withLooseEquals(() -> {
assertThat(i.tetrate(1)).isEqualTo(i);
assertThat(i.tetrate(2)).isEqualTo(
BigComplexNumber.of(
of(".2078795763507619085469556198349787700338778416317696080751358830554198772854821397886002778654260353")
)
);
});
}

@Override
public Arbitrary<BigComplexNumber> elements() {
Arbitrary<Double> real = Arbitraries.doubles().between(-100, 100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,33 @@ public void pow() {
BigDecimalElement base = of(300);
BigDecimalElement exponent = of(-30.1);
assertThat(base.pow(exponent).doubleValue()).isNotEqualTo(0d);
}

@Test
public void tetration() {

BigDecimalElement two = of(2);

withLooseEquals(() -> {

assertThat(two.tetrate(2)).isEqualTo(of(4));
assertThat(two.tetrate(3)).isEqualTo(of(16));
assertThat(two.tetrate(4)).isEqualTo(of(65_536));
assertThat(two.tetrate(5)).isEqualTo(of("2.003529930406846464979072351560255750447825475569751419265016973710894059556311453089506130880933348E+19728"));

});


BigDecimalElement three = of(3);

withLooseEquals(() -> {
assertThat(three.tetrate(2)).isEqualTo(of(27));
assertThat(three.tetrate(3)).isEqualTo(of("7625597484987"));
});

}


@Property
public void timesDouble(
@ForAll(ELEMENTS) BigDecimalElement e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ default E pow(E exponent) throws IllegalPowerException, OverflowException {
}
}

default E tetrate(int n) {
if (n < 0) {
throw new IllegalPowerException("");
}
if (n == 0) {
return getStructure().one();
}
final E t = (E) this;
E e = t;
while (--n > 0) {
e = t.pow(e);
}
return e;
}

E exp();

E ln() throws IllegalLogarithmException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public interface FieldElement<E extends FieldElement<E>> extends
Field<E> getStructure();

@Override
@NonAlgebraic("Cannot divide be zero")
@NonAlgebraic(reason = NonAlgebraic.Reason.SOME, value="Cannot divide be zero")
default E dividedBy(E divisor) throws ReciprocalException {
return DivisionRingElement.super.dividedBy(divisor);
}

@Override
@NonAlgebraic("Zero has no reciprocal")
@NonAlgebraic(reason = NonAlgebraic.Reason.SOME, value="Zero has no reciprocal")
E reciprocal() throws ReciprocalException;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ default E pow(int n) {
return MultiplicativeMonoidElement.super.pow(n);
}



@NonAlgebraic
default E dividedBy(E divisor) throws ReciprocalException {
return times(divisor.reciprocal());
Expand Down

0 comments on commit 434c655

Please sign in to comment.