log(x: Number | Producer, base: Number | Producer | None = None) -> Expression

Calculates the logarithm of a number. If base is None, the natural logarithm is used (base e). Must be called in a rule or query context.


xNumber or ProducerThe number to take the logarithm of.
baseNumber or Producer or NoneThe base of the logarithm. If None, the natural logarithm is used (base e).


An Expression object.


import relationalai as rai
from relationalai.std import alias
from relationalai.std.math import log

# Create a model with a `Person` type.
model = rai.Model("people")
Person = model.Type("Person")

# Add some people to the model.
with model.rule():
    Person.add(name="Alice", age=9)
    Person.add(name="Bob", age=16)
    Person.add(name="Charlie", age=25)

# What is the log base 2 of each person's age?
with model.query() as select:
    p = Person()
    age_log2 = log(p.age, base=2)
    response = select(p.name, alias(age_log2, "age_log2"))

# Output:
#       name  age_log2
# 0    Alice  3.169925
# 1      Bob  4.000000
# 2  Charlie  4.643856