factorial()#
relationalai.std.math
#factorial(number: Number|Producer) -> Expression
Calculates the factorial of number
.
Only defined for integer values between 0 and 33, inclusive.
If number
is a Producer
, factorial()
filters out any invalid values from the producer.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
number | Producer or Python Number object | The non-negative integer to calculate the factorial of. |
Returns#
An Expression
object.
Example#
Use factorial()
to calculate the factorial of a number:
#import relationalai as rai
from relationalai.std import math
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Person = model.Type("Person")
with model.rule():
Person.add(id=1).set(name="Alice", age=20)
Person.add(id=2).set(name="Bob", age=-1) # Negative age
Person.add(id=3).set(name="Carol", age="30") # Non-integer age
# =======
# EXAMPLE
# =======
# Set an age_factorial property to the factorial of each person's age.
with model.rule():
person = Person()
person.set(age_factorial=math.factorial(person.age))
# Since factorial() filters out non-integer and negative values, the age_factorial
# property is not set for Bob or Carol.
with model.query() as select:
person = Person()
response = select(person.id, person.name, person.age_factorial)
print(response.results)
# id name age_factorial
# 0 1 Alice 2.432902e+18
# 1 2 Bob NaN
# 2 3 Carol NaN
Note that factorial()
only accepts input values less than or equal to 33.
Integer values greater than 33 are filtered out:
## Add a person with age=34
with model.rule():
Person.add(id=4).set(name="Derek", age=34)
# Since 34 is greater than 33, the age_factorial property is not set for Derek.
with model.query() as select:
person = Person()
response = select(person.id, person.name, person.age_factorial)
print(response.results)
# id name age_factorial
# 0 1 Alice 2.432902e+18
# 1 2 Bob NaN
# 2 3 Carol NaN
# 3 4 Derek NaN