-
関連項目
- CompiledCodeFunction
- Function
- Typed
- TypeOf
- KernelEvaluate
- KernelFunction
- DownValuesFunction
- $CompilerEnvironment
- CreateCompilerEnvironment
- FunctionDeclaration
- TypeDeclaration
- FunctionCompileExport
- FunctionCompileExportLibrary
- FunctionCompileExportString
- FunctionCompileExportByteArray
- TargetSystem
- $TargetSystems
- $SystemID
- Compile
- ExternalFunction
- CompiledComponent
- CompilerRuntimeErrorAction
- 関連するガイド
- テクニカルノート
-
-
関連項目
- CompiledCodeFunction
- Function
- Typed
- TypeOf
- KernelEvaluate
- KernelFunction
- DownValuesFunction
- $CompilerEnvironment
- CreateCompilerEnvironment
- FunctionDeclaration
- TypeDeclaration
- FunctionCompileExport
- FunctionCompileExportLibrary
- FunctionCompileExportString
- FunctionCompileExportByteArray
- TargetSystem
- $TargetSystems
- $SystemID
- Compile
- ExternalFunction
- CompiledComponent
- CompilerRuntimeErrorAction
- 関連するガイド
- テクニカルノート
-
関連項目
純関数からコンパイルされたコード関数を生成する.
FunctionCompile[{f1,f2,…}]
純関数リストからコンパイルされたコード関数のリストを生成する.
FunctionCompile[k1f1,k2f2,…]
Wolfram言語関数の連想からコンパイルされたコード関数の連想を生成する.
FunctionCompile[defs,fspec]
ローカルの補助定義 defs を使う.
FunctionCompile
純関数からコンパイルされたコード関数を生成する.
FunctionCompile[{f1,f2,…}]
純関数リストからコンパイルされたコード関数のリストを生成する.
FunctionCompile[k1f1,k2f2,…]
Wolfram言語関数の連想からコンパイルされたコード関数の連想を生成する.
FunctionCompile[defs,fspec]
ローカルの補助定義 defs を使う.
詳細とオプション
- 関数 f は純関数Functionとして指定されることが多い.Functionオブジェクトの変数はTypedの注釈が付いていることが多い.
- FunctionCompileによって作成されたCompiledCodeFunctionオブジェクトは,コンパイルされていない関数と同じように適切な引数に適用できる.
- f 内でKernelFunctionを使って,低レベルコードにコンパイルせずにWolfram Engineを直接使う関数を指定することができる.
- DownValuesFunctionは f の中でシンボルに添付された定義のコンパイルに使われる.
- 関数 f はコンパイラ環境の宣言として存在する関数の名前として純粋に指定できる.
- FunctionCompile[defs,fspec]で fspec からローカルの補助定義 defs を呼び出すことができる.これらの定義はFunctionCompileへのこの呼出しのみに使用される.
- fspec 内で使われる補助定義は,CompilerEnvironmentオプションを与えることでも与えられる.
- CompiledCodeFunctionオブジェクト内のコードは,FunctionCompileExportLibraryおよび関連関数を使って,外部目的のために出力することができる.
- TargetSystemオプションを使って追加のアーキテクチャを指定すると,コンパイルを再度実行しなくても出力が異なるプラットフォーム間で移動できる.
- 次は,使用可能なオプションである.
-
CompilerEnvironment Automatic コンパイルに使用する定義の環境 CompilerOptions Automatic コンパイルパイプラインのさまざまな部分の詳細オプション CompilerRuntimeErrorAction Automatic 低レベルコードの実行中に回復不可能なエラーが生じた際の動作 ProgressReporting Automatic コンパイルの進行をどのように報告するか TargetSystem Inherited コード生成用のマシンアーキテクチャ UseEmbeddedLibrary False この構造用の共有ライブラリを作成して埋め込む - 次は,TargetSystemの可能な設定である.
-
Automatic 主要なマシンアーキテクチャのための追加的なコードを生成する All 全マシンアーキテクチャのための追加的なコードを生成する trg マシンアーキテクチャ trg のための追加的なコードを生成する {trg1,trg2,…} マシンアーキテクチャ{trg1,trg2,…}のための追加的なコードを生成する - TargetSystemの可能な値はリスト$TargetSystemsで与えられる.
- 次は,CompilerRuntimeErrorActionの可能な値である.
-
Automatic コンパイラ固有のコードを回避する場合はWolfram Engineで計算を再実行する "Evaluate" 常にWolfram Engineで計算を再実行する None エラーを表す失敗オブジェクトを返す fun 失敗オブジェクトに fun を適用してその結果を返す
例題
すべて開く すべて閉じる例 (6)
Functionをコンパイルされたコード関数にコンパイルする:
cf = FunctionCompile[Function[Typed[arg, "MachineInteger"], arg + 1]]CompiledCodeFunctionは正しいタイプの引数で評価する:
cf[20]cf[True, 1.5]コンパイルされたコードでは多くの一般的な操作がサポートされている:
cf = FunctionCompile[Function[Typed[len, "MachineInteger"], Table[i, {i, len}]]]cf[20]cf = FunctionCompile[Function[Typed[n, "MachineInteger"], Nest[Sin, 10.1, n]]]cf[ 200]InformationはCompiledCodeFunctionの型が抽出できる:
cf = FunctionCompile[Function[Typed[arg, "MachineInteger"], arg + 1]];
Information[cf, "Type"]いくつかの関数はFunctionCompileでコンパイルできる:
cfs = FunctionCompile[{Function[Typed[arg, "Integer64"], arg + 1], Function[Typed[arg, "Integer64"], arg - 1]}]cfs[[1]][10]cfs = FunctionCompile[<|"f1" -> Function[Typed[arg, "Integer64"], arg + 1], "f2" -> Function[Typed[arg, "Integer64"], arg - 1]|>]cfs[[1]][10]FunctionDeclarationで補助定義が与えられる:
dec = FunctionDeclaration[AddTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2 + arg]];
cf = FunctionCompile[dec, Function[Typed[arg, "Integer64"], AddTwo[AddTwo[arg]]]]cf[ 200]TypeDeclarationで新たな型が加えられる:
dec = TypeDeclaration["Product", "simpleProd", <|"field1" -> "MachineInteger", "field2" -> "Boolean"|>];製品型をインスタンス化しフィールドを抽出する関数をコンパイルする:
cf = FunctionCompile[dec, Function[{},
With[{prod = CreateTypeInstance["simpleProd", <|"field1" -> 12, "field2" -> True|>]},
prod["field1"]]
]]cf[ ]FunctionCompileはコンパイラ環境の宣言の名前から関数を作成する:
dec = FunctionDeclaration[squareFun, Typed[{"Real64"} -> "Real64"]@Function[{arg}, arg ^ 2]];cf = FunctionCompile[dec, squareFun]cf[1.5]FunctionCompileが何かをコンパイルできない場合は,通常は,エラーのソースを表示するボタンを含むメッセージが発せられる:
cf = FunctionCompile[Function[Typed[arg, "Real64"], Module[{size = arg < 10}, Sin[size]]]]ソースボタンをクリックすると,通常,エラーの特定に役立つユーザインターフェースが表示される:
[image]スコープ (12)
cf = FunctionCompile[Function[Typed[arg, "Boolean"], If[arg, 1, 2]]];
cf[False]cf = FunctionCompile[Function[Typed[arg, "UnsignedInteger8"], N[arg]]];
cf[20]cf = FunctionCompile[Function[Typed[arg, "Integer16"], 10 + arg]];
cf[20]cf = FunctionCompile[Function[Typed[arg, "Real64"], arg + 2]];
cf[20.2]cf = FunctionCompile[Function[Typed[arg, "Complex128"], arg + 2]];
cf[20.2 + 3.4I]cf = FunctionCompile[Function[Typed[arg, "PackedArray"::["MachineInteger", 1]],
Fold[Plus, arg]]];
cf[{4, 5, 6, 1, 2, 6, 3}]cf = FunctionCompile[Function[Typed[arg, "NumericArray"::["Integer16", 2]],
Max[arg]]];
cf[NumericArray[{{4, 5, 6}, {1, 2, 3}}, "Integer16"]]cf1 = FunctionCompile[Function[Typed[arg, "MachineInteger"], arg + 1]];cf2 = FunctionCompile[Function[{Typed[fun, {"MachineInteger"} -> "MachineInteger"], Typed[arg, "MachineInteger"]}, fun[arg]]];cf2[cf1, 20]Wolfram EngineはKernelFunctionを使って呼び出すことができる.以下はWolfram Engine内の関数を定義する:
f[x_] := x ^ 2KernelFunctionを使ってコンパイルされたコードからユーザ定義の関数を呼び出す:
cf = FunctionCompile[Function[Typed[arg, "MachineInteger"], Typed[KernelFunction[f], {"MachineInteger"} -> "MachineInteger"][arg]]]cf[10]cf = FunctionCompile[Function[{Typed[cond, "Boolean"], Typed[arg, "MachineInteger"]},
Module[{f, g},
f = If[cond, # + 1&, # - 1&];
g = #1[#2]&;
g[f, arg]]]];cf[True, 20]cf[False, 20]cf = FunctionCompile[Function[{Typed[arg1, "MachineInteger"]},
Module[{f = Function[{}, arg1]},
f[]]]];
cf[10]外側のスコープにある変数を参照するネストとした関数は,関数の引数として使うことができる:
cf = FunctionCompile[Function[{Typed[arg1, "MachineInteger"]},
Module[{f1 = Function[{}, arg1], f2 = Function[f, f[]]},
f2[f1]]]]cf[ 10]Printを使って計算の内側を見る:
cf = FunctionCompile[Function[{Typed[arg1, TypeSpecifier["PackedArray"]["MachineInteger", 1]]},
Map[(Print[#];# ^ 2)&, arg1]]];
cf[{5, 6, 2, 1, 6, 7}]CompiledCodeFunctionを他のCompiledCodeFunction式に引数として渡すことができる.
cf1 = FunctionCompile[Function[{Typed[arg, "MachineInteger"]}, If[EvenQ[arg], arg + 1, arg - 1]]]cf1[10]cf2 = FunctionCompile[Function[{Typed[fun, { "MachineInteger"} -> "MachineInteger"], Typed[lim, "MachineInteger"]}, Table[fun[i], {i, lim}]]]cf2[cf1, 100]:=を使う関数を操作して宣言のためにDownValuesを設定する:
fun1[x_] :=
Echo[x ^ 2]FunctionDeclarationは関数に名前と型を与え実装がDownValuesから来ることを示す:
decl = FunctionDeclaration[fun1, Typed[ {"MachineInteger"} -> "MachineInteger"]@DownValuesFunction[fun1]];func = Function[{Typed[len, "Integer64"]}, Table[ fun1[i], {i, len}]];
cf = FunctionCompile[ decl, func];cf[5]CompiledCodeFunctionには,同じアーキテクチャのマシン上で直列化と非直列化を可能にする低レベルのコードが含まれている:
cf = FunctionCompile[Function[{Typed[arg, "MachineInteger"]}, arg]];
Information[cf, "LLVMBinary"]コンパイルの結果はOnceを使って保存できる:
cf = Once[FunctionCompile[Function[{Typed[arg, "MachineInteger"]}, arg + 2]], "Notebook"];
cf[10]cf = Once[FunctionCompile[
FunctionDeclaration[flowCompute, Typed[{"Real64", "Real64"} -> "PackedArray"::["Real64", 2]]@Function[{x, y},
Module[{z},
z = 1 - 1 / (x + I y) ^ 2;
{{x, y}, {Re[z], -Im[z]}}
]]]
,
Function[{Typed[x0, "Real64"], Typed[x1, "Real64"], Typed[y0, "Real64"], Typed[y1, "Real64"]}, Table[Flatten[ Table[{flowCompute[x + d / 4, y], flowCompute[x + d / 4, -y]}, {x, x0, x1}, {y, y0, y1}], 2] , {d, 0, 3, 0.3}]]
], "Notebook"];ListVectorPlot[First[cf[-7, 5, 0.5, 3]]]オプション (6)
CompilerEnvironment (1)
env = CreateCompilerEnvironment[];
CompilerEnvironmentAppendTo[env, FunctionDeclaration[addTwo, Typed[{"Integer64"} -> "Integer64"]@Function[arg, 2arg]]];cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, addTwo[arg]], CompilerEnvironment -> env]cf[10]CompilerRuntimeErrorAction (1)
低レベルコードの実行中のエラー動作はCompilerRuntimeErrorActionによって支配される:
cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, x + y], CompilerRuntimeErrorAction -> Automatic]cf[250, 250]関数がコンパイラに特有のコードを含む場合,関数は評価器で再実行はされない:
cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, Module[ {ptr = ToRawPointer[x + y]}, FromRawPointer[ptr]]], CompilerRuntimeErrorAction -> Automatic];
cf[250, 250]cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, Module[ {ptr = ToRawPointer[x + y]}, FromRawPointer[ptr]]], CompilerRuntimeErrorAction -> "Evaluate"];
cf[250, 250]Noneの設定は,決して評価器で再実行されない:
cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, x + y], CompilerRuntimeErrorAction -> None];
cf[250, 250]cf = FunctionCompile[Function[{Typed[x, "UnsignedInteger8"], Typed[y, "UnsignedInteger8"]}, x + y], CompilerRuntimeErrorAction -> Framed];
cf[250, 250]ProgressReporting (1)
FunctionCompile[ Function[Typed[arg, "PackedArray"::["Real64", 1]], BinCounts[arg, 1.5]]]ProgressReportingオプションをFalseにすると,これは表示されなくなる.
ProgressReportingのデフォルト値はAutomaticであるが,これは大域設定の$ProgressReportingが使われることを意味する.これがFalseに設定されていると,進捗状況は報告されない.
TargetSystem (2)
現行プラットフォームの追加のマシンアーキテクチャ上で実行できるコンパイルされたコード関数を作る:
cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, arg], TargetSystem -> {"Windows-x86-64"}]Information[cf, "LLVMBinary"]可能なすべてのマシンアーキテクチャプラットフォーム上で実行できるコンパイルされたコード関数を作成する:
cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, arg], TargetSystem -> All]Information[cf, "LLVMBinary"]コンパイルされたコード関数は,これらの$SystemID設定を持つマシンに移動でき,もとのコードを再コンパイルしなくても実行できるようになる.
CreateCompilerEnvironmentを使って複数のターゲットシステムが生成できる点に注意のこと.こうすると,クロスコンパイルの時間が短縮できる.
UseEmbeddedLibrary (1)
現行プラットフォーム用の共有ライブラリを埋め込むCompiledCodeFunctionを作成する:
cf = FunctionCompile[Function[{Typed[arg, "Integer64"]}, arg], UseEmbeddedLibrary -> True]cf[[1]]["EmbeddedLibraries"]このライブラリはコード関数がファイルからロードされる際に使われる.こうするとロードが速くなる:
cf>>comp.wl;
cf1 = <<comp.wlcf1[10]特性と関係 (1)
TypeOfは,その引数型を判定するので,何かがコンパイルされるかどうかをチェックする簡単な方法である:
TypeOf[Function[{Typed[x, "Real64"], Typed[y, "Integer64"]}, x + y]]考えられる問題 (4)
エラー (3)
低レベルコードを含む関数の計算中にエラーが出てコンパイラ固有のコードを回避する場合,計算はWolfram Engineで実行される:
cf = FunctionCompile[Function[Typed[arg, "Real64"], Sqrt[arg]]]cf[10.5]以下では関数の実行中にエラーになった.計算は中断され,メッセージが出され,結果の計算にはWolfram Engineが使われた:
cf[-10.2]cf = FunctionCompile[Function[{Typed[arg, "Integer8"]}, arg + 10]]Typedを使って特定の型を指定する:
cf = FunctionCompile[Function[{Typed[arg, "Integer8"]}, arg + Typed[10, "Integer8"]]]CompiledCodeFunctionから関数を返すことはサポートされていない:
FunctionCompile[Function[{}, Function[Typed[arg, "MachineInteger"], arg + 1]]]関数の作成 (1)
FunctionCompileは宣言が複数ある場合は関数が作れない:
dec1 = FunctionDeclaration[squareFun, Typed[{"Real64"} -> "Real64"]@Function[{arg}, arg ^ 2]];
dec2 = FunctionDeclaration[squareFun, Typed[{"Integer64"} -> "Integer64"]@Function[{arg}, arg ^ 2]];cf = FunctionCompile[{dec1, dec2}, squareFun]Typedが使われているならコンパイルできる:
cf = FunctionCompile[{dec1, dec2}, Typed[squareFun, {"Real64"} -> "Real64"]]多形の宣言がある場合,FunctionCompileは関数が作れない:
dec = FunctionDeclaration[squareFun, Typed[ForAllType[a, {a} -> a]]@Function[{arg}, arg ^ 2]];cf = FunctionCompile[dec, squareFun]Typedが使われているならコンパイルできる:
cf = FunctionCompile[dec, Typed[squareFun, {"Real64"} -> "Real64"]]おもしろい例題 (2)
cf = FunctionCompile[Function[{Typed[arg, "MachineInteger"]},
Module[{factFun},
factFun = Function[{arg1},
If[arg1 === 1, 1, arg1 * factFun[arg1 - 1]]];
factFun[arg]]]]cf[10]シンボルの宣言を使う宣言は,宣言が複数回使われる際に便利である:
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]関連項目
CompiledCodeFunction Function Typed TypeOf KernelEvaluate KernelFunction DownValuesFunction $CompilerEnvironment CreateCompilerEnvironment FunctionDeclaration TypeDeclaration FunctionCompileExport FunctionCompileExportLibrary FunctionCompileExportString FunctionCompileExportByteArray TargetSystem $TargetSystems $SystemID Compile ExternalFunction CompiledComponent CompilerRuntimeErrorAction
テクニカルノート
関連するガイド
-
▪
- C/C++言語インターフェース ▪
- 外部プログラムの呼出し ▪
- 外部言語インターフェース ▪
- コードのコンパイル ▪
- コンパイルの型 ▪
- GPU計算 ▪
- データ構造 ▪
- 調整とデバッグ ▪
- NVIDIAを使ったGPU計算 ▪
- Appleを使ったGPU計算
テキスト
Wolfram Research (2019), FunctionCompile, Wolfram言語関数, https://reference.wolfram.com/language/ref/FunctionCompile.html (2021年に更新).
CMS
Wolfram Language. 2019. "FunctionCompile." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/FunctionCompile.html.
APA
Wolfram Language. (2019). FunctionCompile. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FunctionCompile.html
BibTeX
@misc{reference.wolfram_2026_functioncompile, author="Wolfram Research", title="{FunctionCompile}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/FunctionCompile.html}", note=[Accessed: 17-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_functioncompile, organization={Wolfram Research}, title={FunctionCompile}, year={2021}, url={https://reference.wolfram.com/language/ref/FunctionCompile.html}, note=[Accessed: 17-November-2026]}