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 ^ 2その下向きの値を使って整数型の場合のコンパイルされた関数を作成する:
funcComp = 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 + y異なる型の2つの宣言を行い,それぞれを使う関数をコンパイルする:
decls = {
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 + y多様型の宣言を作り,これを異なる型に使用する関数をコンパイルする:
decl = 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]Condition (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]]]列パターン (1)
テクニカルノート
関連するガイド
テキスト
Wolfram Research (2022), DownValuesFunction, Wolfram言語関数, https://reference.wolfram.com/language/ref/DownValuesFunction.html (2025年に更新).
CMS
Wolfram Language. 2022. "DownValuesFunction." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2025. https://reference.wolfram.com/language/ref/DownValuesFunction.html.
APA
Wolfram Language. (2022). DownValuesFunction. Wolfram Language & System Documentation Center. Retrieved from 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: 13-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: 13-September-2026]}