relationalai.std.math.log()
log(x: Number|Producer, base: Number|Producer|None = None) -> Expression
Calculates the logarithm of x
with the specified base
.
If base
is None
, the natural logarithm (base e
) is used.
If x
or base
is a Producer
, log()
filters out any non-numeric or negative values from the producer.
Must be called in a rule or query context.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
x | Producer or Python Number object | The number to take the logarithm of. |
base | Producer or Python Number object or None | The base of the logarithm. If None , the natural logarithm (base e ) is used. |
Returns
Section titled “Returns”An Expression
object.
Example
Section titled “Example”Use log()
to calculate the logarithm of a number with a specified base:
import relationalai as raifrom relationalai.std import math
# =====# SETUP# =====
model = rai.Model("MyModel")Person = model.Type("Person")
with model.rule(): Person.add(id=1).set(name="Alice", age=9) Person.add(id=2).set(name="Bob", age=-10) # Negative age Person.add(id=3).set(name="Carol", age="INVALID") # Non-numeric age
# =======# EXAMPLE# =======
# Set age_log and age_log2 properties to the natural and base-2 logarithms of# each person’s age.with model.rule(): person = Person() person.set(age_log=math.log(person.age)) person.set(age_log2=math.log(person.age, base=2))
# Since log() filters out negative and non-numeric values, the age_log and# age_log2 properties are not set for Bob or Carol.with model.query() as select: person = Person() response = select(person.id, person.name, person.age_log2)
print(response.results)# id name age_log2# 0 1 Alice 3.169925# 1 2 Bob NaN# 2 3 Carol NaN