根を多項式に展開する


8

チャレンジ

スペースで区切られた多項式の根を入力として、多項式の拡張形式を出力します。

たとえば、入力

1 2

この方程式を表します。

(x-1)(x-2)

そして出力する必要があります:

x^2-3x+2

出力の正確な形式は重要ではありません。

1x^2+-3x^1+2x^0

または:

0 0 0
1x^3+0x^2+0x^1+0

または:

3 14 15 92
1x^4+-124x^3+3241x^2+-27954x^1+57960

スコアリング/ルール

  • eval といいねが許可されていません。
  • Pythonの任意のバージョンまたは他の言語を使用できます。

組み込みのようなものはnumpy.polyどうですか?
— Dennis

@Dennis numpyは組み込みではないと思います!
— アリカンジル2016年

Python + NumPyの回答は一般的に受け入れられますが、それは重要ではありません。根を多項式係数に変換する関数を使用できますか?あなたがevalを禁止したので、私は尋ねています、そしてそれはevalよりかなり強力です。
— Dennis

@デニスそれはほとんど全体が思う!しかし、どうぞ!同じ機能がほとんどの言語に組み込まれているためです。
— アリカンジル2016年

根が整数であると仮定できますか?それらが非負の整数であると仮定できますか?
— 誇り高いハスケラー2016年

回答:


3

ゼリー、15バイト

Æṛ‘Ė’Uj€“x^”j”+

これはÆṛ、与えられた根を持つモニック多項式の係数を構築するために使用します。オンラインでお試しください!

使い方

Æṛ‘Ė’Uj€“x^”j”+  Main link. Argument: A (list of roots)

Æṛ               Yield the coefficients of a monic polynomial with roots A.
  ‘              Increment each root by 1.
   Ė             Enumerate the roots, yielding
                 [[1, coeff. of x^0 + 1], ... [n + 1, coeff. of x^n + 1]].
    ’            Decrement all involved integers, yielding
                 [[0, coeff. of x^0], ... [n, coeff. of x^n]].
     U           Upend to yield [[coeff. of x^0, 0], ... [coeff. of x^n, n]].
      j€“x^”     Join each pair, separating by 'x^'.
            j”+  Join the pairs, separating by '+'.

代替バージョン、24バイト

1WW;ð0;_×µ/‘Ė’Uj€“x^”j”+

これは多項式関連の組み込みを使用しません。オンラインでお試しください!

使い方

1WW;ð0;_×µ/‘Ė’Uj€“x^”j”+  Main link. Argument: A (list of roots)

1WW                       Yield [[1]].
   ;                      Concatenate with A.
    ð    µ/               Reduce [[1]] + A by the following, dyadic chain:
     0;                     Prepend a zero to the left argument (initially [1]).
                            This multiplies the left argument by "x".
        ×                   Take the product of both, unaltered arguments.
                            This multiplies the left argument by "r", where r is
                            the root specified in the right argument.
      _                     Subtract.
                            This computes the left argument multiplied by "x-r".
           ‘Ė’Uj€“x^”j”+  As before.

4

MATL、29バイト

ljU"1@_hX+]tn:Pqv'+%gx^%g'wYD

入力は根を持つ配列です。

編集:

  • (2016年5月20日):X+機能がY+含まれているため、この機能は削除されました。したがって、上記のコードに置き換えるX+ことでY+。
  • (2017年9月29日):YD関数の変更によりw、上記のコードを削除する必要があります。

次のリンクには、これらの変更が含まれています。

オンラインでお試しください!

説明

これは、フォームの条件で繰り返さ畳み込みを適用するルートです。[1, -r]r

l          % push number 1
jU         % take input string. Convert to number array
"          % for each root r
  1        %   push number 1
  @_       %   push -r
  h        %   concatenate horizontally
  X+       %   convolve. This gradually builds array of coefficients
]          % end for each
tn:Pq      % produce array [n-1,n-2,...,0], where n is the number of roots
v          % concatenate vertically with array of coefficients
'+%gx^%g'  % format string, sprintf-style
w          % swap
YD         % sprintf. Implicitly display

2

Ruby、155バイト

無名関数、入力は根の配列です。

最初に最も低い電力から印刷するため、呼び出しf[[1,2]](関数をに割り当てたと想定f)は文字列を返します"2x^0+-3x^1+1x^2"。

->x{j=-1
x.map{|r|[-r,1]}.reduce{|a,b|q=a.map{|c|b=[0]+b
b.map{|e|e*c}[1..-1]}
q.pop.zip(*q).map{|e|(e-[p]).reduce(:+)}}.map{|e|"#{e}x^#{j+=1}"}.join('+')}

1

Python 3、453バイト(スペースの削除など)-> 392バイト

import functools
import operator
print([('+'.join(["x^"+str(len(R))]+[str(q)+"x^"+str(r)if r>0else"{0:g}".format(q)for r,q in enumerate([sum(functools.reduce(operator.mul,(-int(R[n])for n,m in enumerate(j)if int(m)==1),1)for j in[(len(R)-len(bin(i)[2:]))*'0'+bin(i)[2:]for i in range(1,2**len(R))]if sum(1-int(k) for k in j)==p)for p in range(len(R))]) ][::-1]))for R in[input().split()]][0])

このリンクを確認してください。これら2つのインポートの背後にある理由を理解するのに役立ちます。


余計な空白をたくさん取り除くことができます
— 誇り高いHaskeller

@proudhaskellerそうですね!ルールを変更したので、自分の答えを変更するのを忘れていました。
— アリカンジル2016年

1
from operator import*、from functools import*数バイトを節約
— shooqie 2016年

import functools,operator
— CalculatorFeline

0

Haskell、99

l%r=zipWith(-)(0:l)$map(r*)l++[0]
f e='0':do(c,i)<-zip(foldl(%)[1]e)[0..];'+':show c++"x^"++show i

最初に低いパワーを印刷し、最初に追加のパワーを印刷し0+ます。例えば:

>f [1,1]
"0+1x^0+-2x^1+1x^2"

この関数は、たたみ込みのように徐々に根を追加して係数を計算しますが、組み込みはありません。

次に、リストモナドを使用してconcat、さまざまな単項式のすべてを暗黙的に使用します。


0

セージ、38バイト

lambda N:prod(x-t for t in N).expand()

オンラインでお試しください

これは、ルートの反復可能オブジェクトを入力として取り、積を計算して(x-x_n) for x_n in rootsから展開する、名前のないラムダを定義します。


0

Mathematica、26バイト

Expand@Product[x-k,{k,#}]&

Mathematicaには強力な多項式ビルトインがあります。

使用法

  f = Expand@Product[x-k,{k,#}]&
  f@{3, 14, 15, 92}
x^4 - 124 x^3 + 3241 x^2 - 27954 x + 57960

0

JavaScript(ES6)、96バイト

a=>a.map(x=>r.map((n,i)=>(r[i]-=x*a,a=n),++j,r.push(a=0)),r=[j=1])&&r.map(n=>n+`x^`+--j).join`+`
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.