Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various improvements for the log operators #401

Open
wants to merge 3 commits into
base: 2024-06
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public GamaPopulation(final IMacroAgent host, final ISpecies species) {
/*
* PATRICK TAILLANDIER: the problem of having the host here is that depending on the simulation the hashcode
* will be different... and this hashcode is very important for the manipulation of GamaMap thus, having two
* different hashcodes depending on the simulation makes ensure the replication of simulation So I remove the
* different hashcodes depending on the simulation ensures the replication of simulation. So I remove the
* host for the moment.
*/
/*
Expand Down
78 changes: 73 additions & 5 deletions gama.core/src/gama/gaml/operators/Maths.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import gama.annotations.precompiler.GamlAnnotations.test;
import gama.annotations.precompiler.GamlAnnotations.usage;
import gama.core.common.interfaces.IKeyword;
import gama.core.runtime.GAMA;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Getting better: Primitive Obsession
The ratio of primitive types in function arguments decreases from 82.95% to 81.56%, threshold = 30.0%

import gama.core.runtime.IScope;
import gama.core.runtime.exceptions.GamaRuntimeException;
import gama.core.util.matrix.IMatrix;
Expand Down Expand Up @@ -752,7 +753,7 @@ public static Double fact(final Integer n) {
equals = "1.0"),
see = "exp")
public static Double ln(final IScope scope, final Double x) {
if (x <= 0) throw GamaRuntimeException.warning("The ln operator cannot accept negative or null inputs", scope);
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The ln operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log(x);
}
Expand All @@ -777,7 +778,7 @@ public static Double ln(final IScope scope, final Double x) {
value = "ln(1)",
equals = "0.0"))
public static Double ln(final IScope scope, final Integer x) {
if (x <= 0) throw GamaRuntimeException.warning("The ln operator cannot accept negative or null inputs", scope);
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The ln operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log(x);
}
Expand Down Expand Up @@ -805,7 +806,7 @@ public static Double ln(final IScope scope, final Integer x) {
equals = "1.0"),
see = "ln")
public static Double log(final IScope scope, final Double x) {
if (x <= 0) throw GamaRuntimeException.warning("The ln operator cannot accept negative or null inputs", scope);
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The log operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log10(x.doubleValue());
}
Expand All @@ -830,11 +831,78 @@ public static Double log(final IScope scope, final Double x) {
value = "log(1)",
equals = "0.0"))
public static Double log(final IScope scope, final Integer x) {
if (x <= 0) throw GamaRuntimeException.warning("The ln operator cannot accept negative or null inputs", scope);
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The log operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log10(x);
}


@operator (
value = "log",
can_be_const = true,
category = { IOperatorCategory.ARITHMETIC },
concept = {})
@doc (
value = "returns the logarithm in base b of the operand.",
examples = @example (
value = "log(100, 100)",
equals = "1.0"))
public static Double log(final IScope scope, final Integer x, final Integer b) {
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The log operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log(x)/Math.log(b);
}

@operator (
value = "log",
can_be_const = true,
category = { IOperatorCategory.ARITHMETIC },
concept = {})
@doc (
value = "returns the logarithm in base b of the operand.",
examples = @example (
value = "log(100, 100.0)",
equals = "1.0"))
public static Double log(final IScope scope, final Integer x, final Double b) {
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The log operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log(x)/Math.log(b);
}

@operator (
value = "log",
can_be_const = true,
category = { IOperatorCategory.ARITHMETIC },
concept = {})
@doc (
value = "returns the logarithm in base b of the operand.",
examples = @example (
value = "log(100.0, 100.0)",
equals = "1.0"))
public static Double log(final IScope scope, final Double x, final Double b) {
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The log operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log(x)/Math.log(b);
}


@operator (
value = "log",
can_be_const = true,
category = { IOperatorCategory.ARITHMETIC },
concept = {})
@doc (
value = "returns the logarithm in base b of the operand.",
examples = @example (
value = "log(100.0, 100)",
equals = "1.0"))
public static Double log(final IScope scope, final Double x, final Integer b) {
if (x <= 0) GAMA.reportAndThrowIfNeeded(scope, GamaRuntimeException.warning("The log operator cannot accept negative or null inputs", scope), false);
// return Double.MAX_VALUE; // A compromise...
return Math.log(x)/Math.log(b);
}


/**
* Negate.
*
Expand All @@ -848,7 +916,7 @@ public static Double log(final IScope scope, final Integer x) {
category = { IOperatorCategory.ARITHMETIC },
concept = { IConcept.MATH, IConcept.ARITHMETIC })
@doc (
value = "If it is used as an unary operator, it returns the opposite of the operand.",
value = "If it is used as a unary operator, it returns the opposite of the operand.",
masterDoc = true)
@test ("-(-90.0) = 90.0")
public static Double negate(final Double x) {
Expand Down
18 changes: 16 additions & 2 deletions gama.library/models/Toy Models/Life/Life.gaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,22 @@ global torus: torus_environment {
}
//Write the description of the model in the console
action description {
write
'Description. The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is the best-known example of a cellular automaton. The game is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input from humans. One interacts with the Game of Life by creating an initial configuration and observing how it evolves. The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead. Every cell interacts with its eight neighbors, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur: \n\t 1.Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. \n\t 2.Any live cell with more than three live neighbours dies, as if by overcrowding. \n\t 3.Any live cell with two or three live neighbours lives on to the next generation. \n\t 4.Any dead cell with exactly three live neighbours becomes a live cell. The initial pattern constitutes the seed of the system. The first(generation) is created by applying the above rules simultaneously to every cell in the seed�births and deaths happen simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the one before). The rules continue to be applied repeatedly to create further generations.';
write 'Description:' ;
write 'The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970.';
write 'It is the best-known example of a cellular automaton.';
write 'The game is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input from humans.';
write 'One interacts with the Game of Life by creating an initial configuration and observing how it evolves.';
write 'The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, live or dead.';
write 'Every cell interacts with its eight neighbors, which are the cells that are directly horizontally, vertically, or diagonally adjacent.';
write 'At each step in time, the following transitions occur:';
write '\t 1.Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.';
write '\t 2.Any live cell with more than three live neighbours dies, as if by overcrowding.';
write '\t 3.Any live cell with two or three live neighbours lives on to the next generation.';
write '\t 4.Any dead cell with exactly three live neighbours becomes a live cell.';
write 'The initial pattern constitutes the seed of the system.';
write 'The first generation is created by applying the above rules simultaneously to every cell in the seed; births and deaths happen simultaneously,';
write 'and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the one before).';
write 'The rules continue to be applied repeatedly to create further generations.';
}

}
Expand Down
Loading