跳转至

Formula syntax(公式语法)

Quiver lets you plot custom mathematical expressions and specify filter conditions using formulas. We support the following constructs in formulas:

Values

  • Number: In addition to standard numbers such as 6, 3.14159, -128, Quiver also supports scientific notation: 1.602e-19.
  • Plot Reference: Each plot is assigned a parameter name when they are created, and you can reference plots using the $ sign: $A refers to the plot with parameter name A, and $A + $B calculates the sum of the two plots.
  • Boolean Values: Use the Boolean values true and false when using logical operators.
  • Time Reference: Reference time in the formula using the functions secondsSince("time") and millisecondsSince("time"). The time term needs to be in ISO-8601 format ↗. For example, secondsSince("1970-01-01T00:00:00+00:00") evaluates to the number of seconds since January 1st, 1970 in UTC.
  • NaN: Represent an undefined or unrepresentable value, such as nulls.

Output types

Formulas can only output numbers or strings, not booleans. Additionally, the output must be homogenous, meaning that a formula must consistently output only numbers or only strings across all evaluations.

If you need to output boolean-like values, consider these workarounds:

  • For numeric output: Use 1 for true and 0 for false. For example: $A > 0 ? 1 : 0
  • For string output: Use "true" and "false" strings. For example: $A > 0 ? "true" : "false"

Operators

  • Numeric Operators: Quiver supports +, -, *, /, and the remainder operator %: 10 % 7 evaluates to 3. You could also use parentheses () to enforce precedence. These operators produce double precision numbers as results.
  • You cannot use ^ to do power operations. See the pow function below in Built-in Functions to do powers.
  • Comparison Operators: Quiver supports == for equality, != for inequality, and >, <, >=, <= for comparisons. These operators produce Boolean values as results.
  • Logical Operators: You can combine Boolean values using logical AND (&&) and logical OR (||) operators. Quiver also supports the logical NOT operator (!), which inverts a Boolean value.
  • Bitwise Operators: You can perform bitwise AND (&), bitwise OR (|), bitwise XOR (^), and bitwise NOT (~) on numeric values. You can also perform bitwise shifts: << for left shift, >> for sign-preserving arithmetic right shift, and >>> for 0-padded logical right shift.

Built-in Functions

Quiver supports the following list of built-in mathematical functions:

Function Name Description
abs(a) absolute value
acos(a) inverse cosine
acosh(a) inverse hyperbolic cosine
asin(a) inverse sine
asinh(a) inverse hyperbolic sine
atan(a) inverse tangent
atanh(a) inverse hyperbolic tangent
atan2(a, b) inverse tangent of two numbers (four-quadrant arc tangent); a is the y coordinate, b is the x coordinate.
cbrt(a) cube root
ceil(a) ceiling
cos(a) cosine
cosh(a) hyperbolic cosine
exp(a) natural exponential
floor(a) floor
isfinite(a) true if argument is neither infinite nor NaN
isnan(a) true if argument is NaN
ln(a) natural logarithm
log10(a) base 10 logarithm
log2(a) base 2 logarithm
max(a, b, ...) maximum of all given inputs
min(a, b, ...) minimum of all given inputs
pow(a, b) a to the power of b
round(a) round to the nearest integer
signum(a) sign function
sin(a) sine
sinh(a) hyperbolic sine
sqrt(a) square root
tan(a) tangent
tanh(a) hyperbolic tangent

Control Flow

Quiver supports the ternary operator a ? b : c to express “if a then b else c”. For example, at each point in time, $A > 0 ? $B * 10 : $C * 10 takes on the value of $B * 10 if the value of $A is greater than 0 at that time; otherwise, it takes on the value of $C * 10.

You can also use the NaN value to return an alternative value where the input series is missing values. For example, at each point in time, @M != NaN ? @M : @K takes on the value of @M if it has a numeric value; otherwise, it takes on the value of @K.

You can use the return and skip statements to stop “early”, and either return a value or indicate that no value should be returned (e.g. to filter out a point).

Language reference

Basic types and assignment

Variables are declared with the var keyword. The = sign is used to assign a value to a variable. Variables are numbers (double precision), booleans, or strings.

var a = 6;
var b = 3.14159;
var c = 1.602e-19; // Exponential notation is supported
var d = 1.602E-19; // Exponents are not case-sensitive

var boolean_example = true;
var boolean_example = false; // re-assign to false

var string_example = "high";

Expressions

Numeric

You can combine literals and variables into more complicated expressions.

The basic mathematical operators are +, -, *, /. To calculate a remainder, use %.

var a = 2 + 2; // a has value 4
a = 3 * 7; // a has value 21
a = 3.5 / 7; // a has value 0.5
a = 10 % 7; // a has value 3

Operator precedence is as expected. You can use parentheses ( ) to group expressions together.

var a = 5 + 3 * 2; // a has value 11
var b = (5 + 3) * 2; // b has value 16

You can also use assignment versions of the five operators listed above.

var a = 10;
a *= 2; // a now has value 20
a += 3; // a now has value 23
a -= 5; // a now have value 18
a /= 3; // a now has value 6
a %= 5; // a now has value 1

Boolean

You can compare numbers using the comparison operators <, <=, >, >=. These operators produce a Boolean (true or false) result.

Equality can be checked with ==, and inequality with !=.

These expressions can then be combined with logical operators && and ||.

You can use the ! operator to invert a Boolean result (true becomes false, false becomes true).

String

Equality can be checked with ==, and inequality with !=.

var a = "high";
var b = "low";

// compare values
var c = a == a; // true
var d = a != b; // true

中文翻译


公式语法

Quiver 允许您绘制自定义数学表达式,并使用公式指定筛选条件。公式支持以下结构:

  • 数字: 除了标准数字(如 63.14159-128)外,Quiver 还支持科学计数法:1.602e-19
  • 绘图引用: 每个绘图在创建时都会被分配一个参数名称,您可以使用 $ 符号来引用绘图:$A 引用参数名为 A 的绘图,$A + $B 计算两个绘图的和。
  • 布尔值: 使用逻辑运算符时,可使用布尔值 truefalse
  • 时间引用: 使用函数 secondsSince("time")millisecondsSince("time") 在公式中引用时间。时间项需采用 ISO-8601 格式 ↗。例如,secondsSince("1970-01-01T00:00:00+00:00") 计算结果为自 UTC 时间 1970 年 1 月 1 日以来的秒数。
  • NaN: 表示未定义或无法表示的值,例如空值(nulls)。

输出类型

公式只能输出数字或字符串,不能输出布尔值。此外,输出必须是同质的,这意味着一个公式在所有评估中必须始终如一地只输出数字或只输出字符串。

如果您需要输出类似布尔值的值,请考虑以下变通方法:

  • 对于数字输出: 使用 1 表示真,0 表示假。例如:$A > 0 ? 1 : 0
  • 对于字符串输出: 使用 "true""false" 字符串。例如:$A > 0 ? "true" : "false"

运算符

  • 数字运算符: Quiver 支持 +-*/ 以及取余运算符 %10 % 7 计算结果为 3。您也可以使用括号 () 来强制执行优先级。这些运算符产生双精度浮点数结果。
    • 不能使用 ^ 进行幂运算。请参阅下文内置函数中的 pow 函数进行幂运算。
  • 比较运算符: Quiver 支持 == 表示相等,!= 表示不等,以及 ><>=<= 进行比较。这些运算符产生布尔值结果。
  • 逻辑运算符: 您可以使用逻辑与(&&)和逻辑或(||)运算符组合布尔值。Quiver 还支持逻辑非运算符(!),用于反转布尔值。
  • 位运算符: 您可以对数值执行按位与(&)、按位或(|)、按位异或(^)和按位非(~)操作。您还可以执行位移操作:<< 表示左移,>> 表示保留符号的算术右移,>>> 表示补零的逻辑右移。

内置函数

Quiver 支持以下内置数学函数列表:

函数名 描述
abs(a) 绝对值
acos(a) 反余弦
acosh(a) 反双曲余弦
asin(a) 反正弦
asinh(a) 反双曲正弦
atan(a) 反正切
atanh(a) 反双曲正切
atan2(a, b) 两个数的反正切(四象限反正切);ay 坐标,bx 坐标。
cbrt(a) 立方根
ceil(a) 向上取整
cos(a) 余弦
cosh(a) 双曲余弦
exp(a) 自然指数
floor(a) 向下取整
isfinite(a) 如果参数既不是无穷大也不是 NaN,则为真
isnan(a) 如果参数是 NaN,则为真
ln(a) 自然对数
log10(a) 以 10 为底的对数
log2(a) 以 2 为底的对数
max(a, b, ...) 所有给定输入的最大值
min(a, b, ...) 所有给定输入的最小值
pow(a, b) ab 次幂
round(a) 四舍五入到最接近的整数
signum(a) 符号函数
sin(a) 正弦
sinh(a) 双曲正弦
sqrt(a) 平方根
tan(a) 正切
tanh(a) 双曲正切

控制流

Quiver 支持三元运算符 a ? b : c 来表示“如果 ab 否则 c”。例如,在每个时间点,$A > 0 ? $B * 10 : $C * 10$A 的值大于 0 时取 $B * 10 的值;否则,取 $C * 10 的值。

您还可以使用 NaN 值在输入序列缺失值时返回一个替代值。例如,在每个时间点,@M != NaN ? @M : @K@M 有数值时取 @M 的值;否则,取 @K 的值。

您可以使用 returnskip 语句来“提前”停止,并返回一个值或指示不应返回任何值(例如,用于过滤掉一个点)。

语言参考

基本类型和赋值

变量使用 var 关键字声明。使用 = 符号为变量赋值。变量可以是数字(双精度浮点数)、布尔值或字符串。

var a = 6;
var b = 3.14159;
var c = 1.602e-19; // 支持指数表示法
var d = 1.602E-19; // 指数不区分大小写

var boolean_example = true;
var boolean_example = false; // 重新赋值为 false

var string_example = "high";

表达式

数字

您可以将字面量和变量组合成更复杂的表达式。

基本的数学运算符是 +-*/。要计算余数,请使用 %

var a = 2 + 2; // a 的值为 4
a = 3 * 7; // a 的值为 21
a = 3.5 / 7; // a 的值为 0.5
a = 10 % 7; // a 的值为 3

运算符优先级符合预期。您可以使用括号 ( ) 将表达式分组。

var a = 5 + 3 * 2; // a 的值为 11
var b = (5 + 3) * 2; // b 的值为 16

您还可以使用上述五个运算符的赋值版本。

var a = 10;
a *= 2; // a 现在的值为 20
a += 3; // a 现在的值为 23
a -= 5; // a 现在的值为 18
a /= 3; // a 现在的值为 6
a %= 5; // a 现在的值为 1

布尔

您可以使用比较运算符 <<=>>= 来比较数字。这些运算符产生布尔值(truefalse)结果。

可以使用 == 检查相等性,使用 != 检查不等性。

然后可以使用逻辑运算符 &&|| 组合这些表达式。

您可以使用 ! 运算符来反转布尔结果(true 变为 falsefalse 变为 true)。

字符串

可以使用 == 检查相等性,使用 != 检查不等性。

var a = "high";
var b = "low";

// 比较值
var c = a == a; // true
var d = a != b; // true