gpt4 book ai didi

compilation - Mathematica : Compile NSolve, NMinimize 等数值函数

转载 作者:行者123 更新时间:2023-12-04 18:10:02 25 4
gpt4 key购买 nike

有没有办法使用 Compile[] 编译这些函数?似乎 Mathematica 在这方面总是失败。

我在 NSolve 和 NMinimize 中有纯数值函数,但它仍然不起作用。

一个小例子是:

tempfunc = Compile[{}, NSolve[x^2 - 5 x + 6 == 0, x]]
tempfunc[]

这会报错...

知道如何编译这些函数并使它们更快吗?

最佳答案

这似乎是不可能的。评价:

Compile`CompilerFunctions[] // Sort

这将给出“可编译”函数的列表。在 Mathematica 10.3 上,它将给出:
{Abs, AddTo, And, Append, AppendTo, Apply, ArcCos, ArcCosh, ArcCot, ArcCoth,
ArcCsc, ArcCsch, ArcSec, ArcSech, ArcSin, ArcSinh, ArcTan, ArcTanh, Arg,
Array, ArrayDepth, Internal`Bag, Internal`BagPart, BitAnd, BitNot, BitOr,
BitXor, Block, BlockRandom, Boole, Break, Cases, Catch, Ceiling, Chop,
Internal`CompileError, System`Private`CompileSymbol, Complement,
ComposeList, CompoundExpression, Conjugate, ConjugateTranspose, Continue,
Cos, Cosh, Cot, Coth, Count, Csc, Csch, Decrement, Delete, DeleteCases,
Dimensions, Divide, DivideBy, Do, Dot, Drop, Equal, Erf, Erfc, EvenQ, Exp,
Fibonacci, First, FixedPoint, FixedPointList, Flatten,
NDSolve`FEM`FlattenAll, Floor, Fold, FoldList, For, FractionalPart, FreeQ,
Gamma, Compile`GetElement, Goto, Greater, GreaterEqual, Gudermannian,
Haversine, If, Im, Implies, Increment, Indexed, Inequality, Compile`InnerDo,
Insert, IntegerDigits, IntegerPart, Intersection, InverseGudermannian,
InverseHaversine, Compile`IteratorCount, Join, Label, Last, Length, Less,
LessEqual, List, Log, Log10, Log2, LogGamma, LogisticSigmoid, LucasL, Map,
MapAll, MapAt, MapIndexed, MapThread, NDSolve`FEM`MapThreadDot, MatrixQ,
Max, MemberQ, Min, Minus, Mod, Compile`Mod1, Module, Most, N, Negative,
Nest, NestList, NonNegative, Not, OddQ, Or, OrderedQ, Out, Outer, Part,
Partition, Piecewise, Plus, Position, Positive, Power, PreDecrement,
PreIncrement, Prepend, PrependTo, Product, Quotient, Random, RandomChoice,
RandomComplex, RandomInteger, RandomReal, RandomSample, RandomVariate,
Range, Re, Internal`ReciprocalSqrt, ReplacePart, Rest, Return, Reverse,
RotateLeft, RotateRight, Round, RuleCondition, SameQ, Scan, Sec, Sech,
SeedRandom, Select, Set, SetDelayed, Compile`SetIterate, Sign, Sin, Sinc,
Sinh, Sort, Sqrt, Internal`Square, Internal`StuffBag, Subtract,
SubtractFrom, Sum, Switch, Table, Take, Tan, Tanh, TensorRank, Throw, Times,
TimesBy, Tr, Transpose, Unequal, Union, Unitize, UnitStep, UnsameQ, VectorQ,
Which, While, With, Xor}

我试过
cnm = Compile[{{a, _Real}},
Block[{v, r},
{v, r} = NMinimize[2 x^2 + x - a, x];
{v, x /. r}
]
]

结果是对未编译的 Mathematica 代码的评估:
In[35]:= cnm[2]

During evaluation of In[35]:= CompiledFunction::cfse: Compiled expression {-2.125,{x->-0.25}} should be a machine-size real number. >>

During evaluation of In[35]:= CompiledFunction::cfex: Could not complete external evaluation at instruction 1; proceeding with uncompiled evaluation. >>

Out[35]= {-2.125, -0.25}

关于compilation - Mathematica : Compile NSolve, NMinimize 等数值函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16296747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com