据我了解您的问题,您有两个多项式,并希望找到它们相等的所有点。
这是一个使用 执行此操作的函数Modelica.Math.Vectors.Utilities.roots
:
首先,您给出两个多项式poly1
和poly2
。查找poly1=poly2
与查找相同poly1-poly2=0
,因此我定义了第三个多项式polyDiff = polyLong-polyShort
,然后将该多项式交给Modelica.Math.Vectors.Utilities.roots
. 它将返回所有根,甚至是复杂的根。
function polyIntersect
input Real[:] poly1={3,2,1,0};
input Real[:] poly2={8,7};
output Real[:,2] intersect;
protected
Integer nPoly1 = size(poly1,1);
Integer nPoly2 = size(poly2,1);
Integer nPolyShort = min(nPoly1, nPoly2);
Integer nPolyLong = max(nPoly1, nPoly2);
Real[nPolyShort] polyShort;
Real[nPolyLong] polyLong;
Real[nPolyLong] polyDiff;
algorithm
if (nPoly1<nPoly2) then
polyShort := poly1;
polyLong := poly2;
else
polyShort := poly2;
polyLong := poly1;
end if;
polyDiff := polyLong;
for i in 0:nPolyShort-1 loop
polyDiff[nPolyLong-i] := polyLong[nPolyLong-i] - polyShort[nPolyShort-i];
end for;
intersect := Modelica.Math.Vectors.Utilities.roots(polyDiff);
end polyIntersect;
上面的代码也可以在这里找到:https ://gist.github.com/thorade/5388205