Movatterモバイル変換


[0]ホーム

URL:


Fast symbolicmultivariate polynomials in R

Overview

Multivariate polynomials are interesting and useful objects. Here Ipresent themvp package which hopefully improves uponprevious R functionality provided by the packagesmultipol,mpoly, andspray. Themvp packagefollowsmpoly in using a symbolic, rather than numeric,representation of a multivariate polynomial; but it offers speedadvantages overmpoly.mvp uses the excellentprint and coercion methods of thempoly package.mvp includes some pleasing substitution idiom not foundelsewhere; it is theoretically comparable in speed to thespray package and I present some timings in the packagevignette.

Themvp package usesC++’s STLmap class for efficiency, which has the downside that theorder of the terms, and the order of the symbols within each term, isundefined. This does not matter as the mathematical value of amultivariate polynomial is unaffected by reordering; and the printmethod (taken frommpoly) does a good job in producinghuman-readable output.

Installation

You can install the released version ofmvp fromCRAN with:

# install.packages("mvp")  # uncomment this to install the packagelibrary("mvp")

Themvp package in use

Creating a multivariate polynomial is straightforward:

X<-as.mvp("1 + a^2 + a*b*c^3")X#> mvp object algebraically equal to#> 1 + a b c^3 + a^2

and arithmetic operations work as expected:

Y<-as.mvp("12*a^2  + b - c^2 + 4*d")X+Y#> mvp object algebraically equal to#> 1 + a b c^3 + 13 a^2 + b - c^2 + 4 dX-3*Y#> mvp object algebraically equal to#> 1 + a b c^3 - 35 a^2 - 3 b + 3 c^2 - 12 dX^2#> mvp object algebraically equal to#> 1 + 2 a b c^3 + 2 a^2 + a^2 b^2 c^6 + 2 a^3 b c^3 + a^4

Substitution uses thesubs() function:

X#> mvp object algebraically equal to#> 1 + a b c^3 + a^2subs(X,a=1)#> mvp object algebraically equal to#> 2 + b c^3subs(X,a=1,b=2)#> mvp object algebraically equal to#> 2 + 2 c^3subs(X,a=1,b=2,c=3)#> [1] 56subs(X+Y,a="1+x^2",b="x+y",c=0)#> mvp object algebraically equal to#> 14 + 4 d + x + 26 x^2 + 13 x^4 + y

Further information

For more detail, see the package vignette

vignette("mvp")


[8]ページ先頭

©2009-2025 Movatter.jp