Skip to content

Commit

Permalink
Some support for golden ratio.
Browse files Browse the repository at this point in the history
  • Loading branch information
mihxil committed Oct 28, 2024
1 parent 3ecf315 commit e85d947
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public S e() {
return of(getElementStructure().e());
}

@Override
public S φ() {
return of(getElementStructure().φ());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Optional;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.meeuw.configuration.ConfigurationService;
import org.meeuw.math.*;
import org.meeuw.math.abstractalgebra.*;
import org.meeuw.math.abstractalgebra.complex.BigComplexNumber;
Expand All @@ -29,6 +28,8 @@
import org.meeuw.math.operators.BasicAlgebraicIntOperator;
import org.meeuw.math.uncertainnumbers.*;

import static org.meeuw.configuration.ConfigurationService.getConfigurationAspect;

/**
* A real number (backend by a big decimal), element of {@link BigDecimalElement}.
* @author Michiel Meeuwissen
Expand All @@ -55,7 +56,10 @@ public class BigDecimalElement implements
* e as a {@link BigDecimalElement}
* @see Utils#e
*/
public static final BigDecimalElement e = new BigDecimalElement(new BigDecimal(Utils.e), new BigDecimal("1e-" + (Utils.e.length() - 1)));
public static final BigDecimalElement e = new BigDecimalElement(
new BigDecimal(Utils.e), new BigDecimal("1e-" + (Utils.e.length() - 1)));



private final BigDecimal value;
private final BigDecimal uncertainty;
Expand Down Expand Up @@ -111,7 +115,10 @@ public int compareTo(@NonNull BigDecimalElement compare) {

@Override
public BigDecimalElement sqr() {
return new BigDecimalElement(value.multiply(value), uncertainty.multiply(uncertainty));
return new BigDecimalElement(
value.multiply(value),
uncertainty.multiply(uncertainty)
);
}

@Override
Expand Down Expand Up @@ -335,7 +342,7 @@ public <F extends AlgebraicElement<F>> Optional<F> castDirectly(Class<F> clazz)

@Override
public boolean eq(BigDecimalElement that) {
return eq(that, Math.round( ConfigurationService.getConfigurationAspect(ConfidenceIntervalConfiguration.class).getSds() + 0.5f));
return eq(that, Math.round( getConfigurationAspect(ConfidenceIntervalConfiguration.class).getSds() + 0.5f));
}

@Override
Expand All @@ -346,7 +353,7 @@ public boolean strictlyEquals(Object o) {
}
@Override
public boolean equals(Object o) {
if (ConfigurationService.getConfigurationAspect(CompareConfiguration.class).isEqualsIsStrict()) {
if (getConfigurationAspect(CompareConfiguration.class).isEqualsIsStrict()) {
return strictlyEquals(o);
} else {
return eq((BigDecimalElement) o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.*;

import org.meeuw.math.Example;
import org.meeuw.math.Utils;
import org.meeuw.math.abstractalgebra.*;
import org.meeuw.math.abstractalgebra.complex.BigComplexNumbers;
import org.meeuw.math.numbers.BigDecimalOperations;
Expand Down Expand Up @@ -83,11 +82,16 @@ public BigDecimalElement atan2(BigDecimalElement y, BigDecimalElement x) {

@Override
public BigDecimalElement pi() {
return BigDecimalElement.of(Utils.PI);
return BigDecimalElement.PI;
}

@Override
public BigDecimalElement e() {
return BigDecimalElement.of(Utils.e);
return BigDecimalElement.e;
}

@Override
public BigDecimalElement φ() {
return (BigDecimalElement.ONE.plus(BigDecimalElement.of(5).sqrt())).dividedBy(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ public RealNumber pi() {
public RealNumber e() {
return RealNumber.of(Math.E);
}
@Override
public RealNumber φ() {
return RealNumber.of((1 + Math.sqrt(5)) / 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,21 @@ protected <C extends AlgebraicStructure<?>> List<OperatorCell> getOperators(Cla
rest.append(o.stringify("", ""));
}
}

for (AlgebraicComparisonOperator o : target.getSupportedComparisonOperators()) {
if (!rest.isEmpty()) {
rest.append(' ');
}
rest.append(o.stringify("", ""));
}

for (AlgebraicIntOperator o : target.getSupportedIntOperators()) {
if (!rest.isEmpty()) {
rest.append(' ');
}
rest.append(o.stringify("x", "n"));
}

if (!rest.isEmpty()) {
ops.add(new OperatorCell(rest).withTitle("other binary operators"));
}
Expand All @@ -308,6 +317,7 @@ protected <C extends AlgebraicStructure<?>> List<OperatorCell> getOperators(Cla
}
}


{
List<String> special = new ArrayList<>();
appendSpecials(special, clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public interface CompleteField<E extends CompleteFieldElement<E>> extends Field<

E e();

default E π() {
return pi();
}

/**
* Golden ratio 1/1 + srt(5)
*/
E φ();

@Override
default NavigableSet<AlgebraicBinaryOperator> getSupportedOperators() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,10 @@ public UncertainReal e() {
return UncertainDoubleElement.of(Math.E, DoubleUtils.uncertaintyForDouble(Math.E));
}

@Override
public UncertainReal φ() {
double phi = (1d + Math.sqrt(5)) / 2;
return UncertainDoubleElement.of(phi, DoubleUtils.uncertaintyForDouble(phi));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@
*/
package org.meeuw.theories.abstractalgebra;

import java.math.MathContext;
import java.util.Optional;

import net.jqwik.api.ForAll;
import net.jqwik.api.Property;

import org.meeuw.configuration.ConfigurationService;
import org.meeuw.math.NonAlgebraic;
import org.meeuw.math.abstractalgebra.*;
import org.meeuw.math.exceptions.IllegalLogarithmException;
import org.meeuw.math.exceptions.OverflowException;
import org.meeuw.math.numbers.MathContextConfiguration;

import static org.meeuw.assertj.Assertions.assertThat;
import static org.meeuw.configuration.ConfigurationService.setConfiguration;
import static org.meeuw.math.operators.BasicAlgebraicBinaryOperator.POWER;
import static org.meeuw.math.operators.BasicAlgebraicUnaryOperator.*;

Expand Down Expand Up @@ -84,4 +88,18 @@ default void cosPi(@ForAll(STRUCTURE) AlgebraicStructure<?> struct) {
assertThat(casted.pi().cos()).isEqTo(casted.one().negation());
}

@Property
default void goldenRatio(@ForAll(STRUCTURE) AlgebraicStructure<?> struct) {
CompleteField<E> casted = (CompleteField<E>) struct;
try {
setConfiguration(builder ->
builder.configure(MathContextConfiguration.class,
(mathContextConfiguration) -> mathContextConfiguration.withContext(new MathContext(4))));
assertThat(casted.φ().sqr()).isEqTo(casted.φ().plus(casted.one()));
} finally {
ConfigurationService.resetToDefaults();

}
}

}

0 comments on commit e85d947

Please sign in to comment.