cos()#
relationalai.std.math
#cos(number: Number|Producer) -> Expression
Calculates the cosine of number
, where number
is specified in radians.
If number
is a Producer
, cos()
filters out any non-numeric values from the producer.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
number | Producer or Python Number object | The angle in radians to calculate the cosine of. |
Returns#
An Expression
object.
Example#
Use cos()
to calculate the cosine of an angle in radians:
#import relationalai as rai
from relationalai.std import math
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Angle = model.Type("Angle")
with model.rule():
Angle.add(id=1).set(value=0)
Angle.add(id=2).set(value=3.14159) # Approximately π
Angle.add(id=3).set(value="INVALID") # Non-numeric value
# =======
# EXAMPLE
# =======
# Set a cos_value property to the cosine of each angle's value.
with model.rule():
angle = Angle()
angle.set(cos_value=math.cos(angle.value))
# Since cos() filters out non-numeric values, the cos_value property
# is not set for the angle with ID 3.
with model.query() as select:
angle = Angle()
response = select(angle.id, angle.cos_value)
print(response.results)
# id cos_value
# 0 1 1.0
# 1 2 -1.0
# 2 3 NaN