DownValuesFunction[sym]
表示一个函数,在编译时使用附加于 sym 的定义.
DownValuesFunction
DownValuesFunction[sym]
表示一个函数,在编译时使用附加于 sym 的定义.
更多信息
- DownValuesFunction 可被直接用在已编译代码中.
- DownValuesFunction 可被用在 FunctionDeclaration 指定的已编译声明中.
- DownValuesFunction 规范通常使用 Typed 给出类型注释.
- DownValuesFunction 被编译时,它会根据符号声明创建一个函数并对其进行编译.
- 使用 DownValuesFunction 的声明可使用多态类型.
- 支持以下模式构造:
-
_ 匹配一个参数 x_ 匹配名称被设为 x 的参数 _h 若标头为 h 则匹配 x_?test 若 test[x] 的结果为 True 则匹配 p/;cond 若 cond 的结果为 True 则匹配 Except[p] 若 p 不匹配则匹配 p1|p2 若 pi 之一匹配则匹配 f[g[p]] 匹配复合表达式(用于惰性表达式和压缩数组) __,___ 匹配一个序列(用于惰性表达式)
范例
打开所有单元 关闭所有单元基本范例 (2)
square[x_] := x ^ 2funcComp = FunctionCompile[
Function[Typed[arg, "MachineInteger"],
Typed[DownValuesFunction[square], {"MachineInteger"} -> "MachineInteger"][arg]
]
]funcComp[100]factorial[0] = factorial[1] = 1;
factorial[n_] := n * factorial[n - 1]用 FunctionDeclaration 将阶乘函数与一个整数类型特征及其下值实现关联起来:
factorialC = FunctionCompile[
FunctionDeclaration[factorial, Typed[{"MachineInteger"} -> "MachineInteger"]@DownValuesFunction[factorial]
],
Function[Typed[arg, "MachineInteger"], factorial[arg]]
]factorialC[10]范围 (13)
多个定义 (1)
f2[x_, y_] /; x > y := y
f2[x_, 2] := x
f2[x_, y_] := x + ycf = FunctionCompile[Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, Typed[DownValuesFunction[f2], {"MachineInteger", "MachineInteger"} -> "MachineInteger"][a1, a2]]]cf[55, 22]cf[1, 2]cf[55, 200]多个声明 (1)
funA[x_, y_] := x + ydecls = {
FunctionDeclaration[funA, Typed[{"MachineInteger", "MachineInteger"} -> "MachineInteger"]@
DownValuesFunction[funA]
],
FunctionDeclaration[funA,
Typed[{"Real64", "Real64"} -> "Real64"]@
DownValuesFunction[funA]
]
};
cfs = FunctionCompile[decls, <|
"int" -> Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]},
funA[a1, a2]
],
"real" -> Function[{Typed[a1, "Real64"], Typed[a2, "Real64"]}, funA[a1, a2]
]
|>]cfs["int"][2, 3]cfs["real"][2.5, 3.25]多态声明 (1)
funB[x_, y_] := x + ydecl = FunctionDeclaration[ funB, Typed[ForAllType[t, {t, t} -> t]]@DownValuesFunction[funB]];
cfs = FunctionCompile[ {decl}, <|"int" -> Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, funB[a1, a2]], "real" -> Function[{Typed[a1, "Real64"], Typed[a2, "Real64"]}, funB[a1, a2]]|>]cfs["int"][2, 3]cfs["real"][2.5, 3.25]递归调用 (1)
fact[0] := 1
fact[n_] := n * fact[n - 1]decl = FunctionDeclaration[fact, Typed[{"MachineInteger"} -> "MachineInteger"]@DownValuesFunction[fact]];
cf = FunctionCompile[{decl}, Function[{Typed[a1, "MachineInteger"]}, fact[a1]]]cf[10]未命名模式 (1)
重复的模式 (1)
repeatedPattern[x_, x_] := {x}
repeatedPattern[x_, y_] := {x, y}cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, Typed[DownValuesFunction[repeatedPattern], {"MachineInteger", "MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1, a2]]]cf[1, 2]cf[3, 3]匹配标头 (1)
headMatch[x_Real] := "Real"
headMatch[x_] := "NotReal"decl = FunctionDeclaration[headMatch, Typed[ForAllType["e", {"e"} -> "String"]]@DownValuesFunction[headMatch]];cf = FunctionCompile[{decl}, Function[{Typed[a1, "MachineInteger"]}, headMatch[a1]]];cf[1]cf = FunctionCompile[{decl}, Function[{Typed[a1, "Real64"]}, headMatch[a1]]];cf[1.5]模式测试 (1)
编译使用 PatternTest 的模式:
patternTest[x_ ? EvenQ] := {0, x}
patternTest[x_] := {1, x}cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"]}, Typed[DownValuesFunction[patternTest], {"MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1]]];cf[2]cf[21]条件 (1)
编译使用 Condition 的模式:
conditionPattern[x_, y_] /; x > y := {y, x}
conditionPattern[x_, y_] := {x, y}cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"], Typed[a2, "MachineInteger"]}, Typed[DownValuesFunction[conditionPattern], {"MachineInteger", "MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1, a2]]];cf[1, 2]cf[7, 3]Except (1)
编译使用 Except 的模式:
exceptPattern[Except[y_ ? EvenQ]] := {y, 0}
exceptPattern[y_] := {y}cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"]}, Typed[DownValuesFunction[exceptPattern], {"MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1]]];cf[20]cf[3]exceptPattern2[y : Except[_ ? EvenQ, y_ /; y > 20]] := {y, 0}
exceptPattern2[y_] := {y}cf = FunctionCompile[Function[{Typed[a1, "MachineInteger"]}, Typed[DownValuesFunction[exceptPattern2], {"MachineInteger"} -> "PackedArray"::["MachineInteger", 1]][a1]]];cf[24]cf[25]cf[3]Alternatives (1)
编译可使用 Alternatives 的模式
alternativesPattern[x : (_Integer | _Real)] := xdecl = FunctionDeclaration[alternativesPattern, Typed[ForAllType["e", {"e"} -> "e"]]@DownValuesFunction[alternativesPattern]];cf = FunctionCompile[{decl}, Function[{Typed[a1, "MachineInteger"]}, alternativesPattern[a1]]];cf[1]cf = FunctionCompile[{decl}, Function[{Typed[a1, "Real64"]}, alternativesPattern[a1]]];cf[1.5]复合模式 (1)
编译支持对某些类型(如 "InertExpression" 和 "PackedArray")使用复合模式:
compoundPattern[{x_, y_}] := x[y]
compoundPattern[x_] := xcf = FunctionCompile[Function[{Typed[a1, "InertExpression"]}, Typed[DownValuesFunction[compoundPattern], {"InertExpression"} -> "InertExpression"][a1]]];cf[{f, x}]cf[{a, b, c}]序列 (1)
编译支持某些针对 "InertExpression" 的序列模式:
sequencePattern[{x_, y__}] := x[y]
sequencePattern[_] := InertExpression[$Failed]cf = FunctionCompile[Function[{Typed[a1, "InertExpression"]}, Typed[DownValuesFunction[sequencePattern], {"InertExpression"} -> "InertExpression"][a1]]];cf[{f, x, y, z}]cf[{f}]sequencePattern2[f[x_, {__, x_}]] := x
sequencePattern2[_] := InertExpression[$Failed]cf = FunctionCompile[Function[{Typed[a1, "InertExpression"]}, Typed[DownValuesFunction[sequencePattern2], {"InertExpression"} -> "InertExpression"][a1]]];cf[f[g, {1, 2, 3, 4, g}]]cf[f[g, {1, 2, 3, 4, h}]]应用 (1)
符号定义 (1)
restrictFlow[ x_, y_] :=
Module[{z},
z = 1 - 1 / (x + I y) ^ 2;
{{x, y}, {Re[z], -Im[z]}}
]引用符号的 FunctionDeclaration:
decl = FunctionDeclaration[restrictFlow, Typed[{"Real64", "Real64"} -> "PackedArray"::["Real64", 2]]@DownValuesFunction[restrictFlow]];func = Function[{Typed[x0, "Real64"], Typed[x1, "Real64"], Typed[y0, "Real64"], Typed[y1, "Real64"]}, Table[Flatten[ Table[{restrictFlow[x + d / 4, y], restrictFlow[x + d / 4, -y]}, {x, x0, x1}, {y, y0, y1}], 2] , {d, 0, 3, 0.3}]];cf = FunctionCompile[decl, func]data = cf[-7, 5, 0.5, 3];ListVectorPlot[First[data]]func[-7, 5, 0.5, 3];//RepeatedTimingcf[-7, 5, 0.5, 3];//RepeatedTimingAnimate[ListVectorPlot[Part[data, i]], {i, 1, Length[data], 1}, SaveDefinitions -> True, AnimationRunning -> False]可能存在的问题 (5)
更新 (1)
DownValuesFunction 使用的是进行编译时使用的定义:
testFun[x_] := x ^ 2func = Function[Typed[arg, "MachineInteger"], Typed[DownValuesFunction[testFun], {"MachineInteger"} -> "MachineInteger"][arg]];
funcComp = FunctionCompile[func];funcComp[10]testFun[x_] := x ^ 3funcComp[10]定义限制 (1)
如果没有适用于所有情况的定义,将添加出现运行时错误如何处理:
fun1[x_ ? Positive] :=
xdecl = FunctionDeclaration[fun1, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun1]];
funcComp = FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun1[arg]]]funcComp[10]funcComp[-10]类型一致性 (1)
fun10[x_ /; x > 10] :=
True
fun10[x_] :=
10decl = FunctionDeclaration[fun10, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun10]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun10[arg]]]fun11[x_ /; x > 1] :=
Sin[x]
fun11[x_] :=
xdecl = FunctionDeclaration[fun11, Typed[{"Real64"} -> "Real64"]@DownValuesFunction[fun11]];
FunctionCompile[decl, Function[Typed[arg, "Real64"], fun11[arg]]]decl = FunctionDeclaration[fun11, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun11]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun11[arg]]]不支持的模式 (1)
fun5[x_, y_ : 10] :=
xdecl = FunctionDeclaration[fun5, Typed[{"Integer64"} -> "Integer64"]@DownValueFunction[fun5]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun5[arg]]]fun8[HoldPattern[y_]] :=
xdecl = FunctionDeclaration[fun8, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun8]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun8[arg]]]fun9[Verbatim[y_]] :=
xdecl = FunctionDeclaration[fun9, Typed[{"Integer64"} -> "Integer64"]@DownValuesFunction[fun9]];
FunctionCompile[decl, Function[Typed[arg, "Integer64"], fun9[arg]]]技术笔记
相关指南
-
▪
- 代码编译
文本
Wolfram Research (2022),DownValuesFunction,Wolfram 语言函数,https://reference.wolfram.com/language/ref/DownValuesFunction.html (更新于 2025 年).
CMS
Wolfram 语言. 2022. "DownValuesFunction." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2025. https://reference.wolfram.com/language/ref/DownValuesFunction.html.
APA
Wolfram 语言. (2022). DownValuesFunction. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/DownValuesFunction.html 年
BibTeX
@misc{reference.wolfram_2026_downvaluesfunction, author="Wolfram Research", title="{DownValuesFunction}", year="2025", howpublished="\url{https://reference.wolfram.com/language/ref/DownValuesFunction.html}", note=[Accessed: 06-September-2026]}
BibLaTeX
@online{reference.wolfram_2026_downvaluesfunction, organization={Wolfram Research}, title={DownValuesFunction}, year={2025}, url={https://reference.wolfram.com/language/ref/DownValuesFunction.html}, note=[Accessed: 06-September-2026]}