creación polinomios

113 days ago by Alex_

def poly(n,m): #poly(n,m) genera aleatoriamente un polinomio con coeficientes enteros positivos, var('x') #menores o iguales a m, para un polinómio de grado n l=[] p=0 for i in range (1,n+2): t=ceil(random()*m) l.append(t) p=p+t*x^(i-1) return p poly(6,7) 
       
2*x^6 + 3*x^5 + 2*x^4 + 6*x^3 + 7*x^2 + 2*x + 7
2*x^6 + 3*x^5 + 2*x^4 + 6*x^3 + 7*x^2 + 2*x + 7
poly(2,7) #ejemplo de un polinomio de grado 2 con coeficientes enteros positivos menores o iguales a 7 
       
x^2 + 7*x + 4
x^2 + 7*x + 4
def poli(n,m): #poli(n,m) genera aleatoriamente un polinomio con var('x') #coeficientes enteros positivos o Negativos, l=[] #menores o iguales a m, para un polinómio de grado n p=0 S=1 for i in range (1,n+2): t=ceil(random()*m) s=random() if s<0.5: S=-1 #S asignará aleatoriamente el signo al nuevo término generado. t=S*t l.append(t) p=p+t*x^(i-1) return p poli(3,11) 
       
-5*x^3 - 11*x^2 - 11*x + 1
-5*x^3 - 11*x^2 - 11*x + 1
poli(3,5) 
       
-2*x^3 - x^2 - 3*x + 2
-2*x^3 - x^2 - 3*x + 2
'''El siguiente programa genera una matriz que contiene k columnas, donde una fila corresponde a polinomios de grado n con coeficientes enteros de valor absoluto a lo más m, y la otra a su correspondiente gráfica''' def polgraf(n,m,k): p=[] for i in range(1,k+2): q=poli(n,m) p=p.append[q] for i in range(1,k+2): u=q[i] gra=plot(u) print gra #w=w.append(gra) #M=matrix(¨) #return M return p polgraf(3,3,4) 
       
Traceback (click to the left of this block for traceback)
...
NameError: global name 'poli' is not defined
Traceback (most recent call last):        for i in range(1,k+2):
  File "", line 1, in <module>
    
  File "/tmp/tmpJnPTd4/___code___.py", line 20, in <module>
    exec compile(u'polgraf(_sage_const_3 ,_sage_const_3 ,_sage_const_4 )
  File "", line 1, in <module>
    
  File "/tmp/tmpJnPTd4/___code___.py", line 10, in polgraf
    q=poli(n,m)
NameError: global name 'poli' is not defined
w=poli(5,18) show(w) show(plot(w)) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-10 \, x^{5} - 16 \, x^{4} - 15 \, x^{3} - 6 \, x^{2} + 16 \, x + 8
\newcommand{\Bold}[1]{\mathbf{#1}}-10 \, x^{5} - 16 \, x^{4} - 15 \, x^{3} - 6 \, x^{2} + 16 \, x + 8
def grafpoli(n,m,k): #grafica k polinomios de grado n con coeficientes menores o iguales a m, en valor absoluto for i in range(k): q=poli(n,m) show(q) show(plot(q)) return grafpoli(3,45,5) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-37 \, x^{3} - 8 \, x^{2} + 30 \, x + 39
\newcommand{\Bold}[1]{\mathbf{#1}}-2 \, x^{3} - 5 \, x^{2} - 45 \, x + 42
\newcommand{\Bold}[1]{\mathbf{#1}}-5 \, x^{3} - 45 \, x^{2} + 17 \, x + 6
\newcommand{\Bold}[1]{\mathbf{#1}}-32 \, x^{3} - 18 \, x^{2} - 34 \, x - 10
\newcommand{\Bold}[1]{\mathbf{#1}}-25 \, x^{3} - 44 \, x^{2} - 24 \, x - 39
\newcommand{\Bold}[1]{\mathbf{#1}}-37 \, x^{3} - 8 \, x^{2} + 30 \, x + 39
\newcommand{\Bold}[1]{\mathbf{#1}}-2 \, x^{3} - 5 \, x^{2} - 45 \, x + 42
\newcommand{\Bold}[1]{\mathbf{#1}}-5 \, x^{3} - 45 \, x^{2} + 17 \, x + 6
\newcommand{\Bold}[1]{\mathbf{#1}}-32 \, x^{3} - 18 \, x^{2} - 34 \, x - 10
\newcommand{\Bold}[1]{\mathbf{#1}}-25 \, x^{3} - 44 \, x^{2} - 24 \, x - 39
def grafpoli(n,m,k): #grafica k polinomios de grado n con coeficientes menores o iguales a m, en valor absoluto g=[] for i in range(k): q=poli(n,m) show(q) show(plot(q)) return grafpoli(3,45,3) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-24 \, x^{3} - 30 \, x^{2} - 20 \, x + 20
\newcommand{\Bold}[1]{\mathbf{#1}}-15 \, x^{3} - 18 \, x^{2} - 10 \, x - 28
\newcommand{\Bold}[1]{\mathbf{#1}}-13 \, x^{3} - 31 \, x^{2} + 8 \, x + 23
\newcommand{\Bold}[1]{\mathbf{#1}}-24 \, x^{3} - 30 \, x^{2} - 20 \, x + 20
\newcommand{\Bold}[1]{\mathbf{#1}}-15 \, x^{3} - 18 \, x^{2} - 10 \, x - 28
\newcommand{\Bold}[1]{\mathbf{#1}}-13 \, x^{3} - 31 \, x^{2} + 8 \, x + 23
grafpoli(3,4,2) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}-3 \, x^{3} - 4 \, x^{2} - x + 3
\newcommand{\Bold}[1]{\mathbf{#1}}-2 \, x^{3} - x^{2} + 4 \, x + 3
\newcommand{\Bold}[1]{\mathbf{#1}}-3 \, x^{3} - 4 \, x^{2} - x + 3
\newcommand{\Bold}[1]{\mathbf{#1}}-2 \, x^{3} - x^{2} + 4 \, x + 3
#generar polinomios mónico a partir de sus raices. Grado del polinomio n, valores de las raices enteras en valor absoluto menor a m def polira(n,m): var('x') l=[] p=1 S=1 for i in range (1,n+1): t=ceil(random()*m) s=random() if s<0.5: S=-1 # S asignará el signo de la raíz, aleatoriamente t=S*t # este será la nueva raíz del polinomio l.append(t) p=p*(x-t) q=expand(p) # genera el polinomio expandido return ['para el polinomio', q, 'las raices son ', l] , plot(q,(x,-m,m), legend_label=q(x)) #muestra la gráfica del polinomio entre -m y m 
       
polira(2,9) 
       
Traceback (click to the left of this block for traceback)
...
AttributeError: 'sage.symbolic.expression.Expression' object has no
attribute 'startswith'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_158.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cG9saXJhKDIsOSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpYPfq2V/___code___.py", line 3, in <module>
    exec compile(u'polira(_sage_const_2 ,_sage_const_9 )
  File "", line 1, in <module>
    
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/misc/displayhook.py", line 174, in displayhook
    print_obj(sys.stdout, obj)
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/misc/displayhook.py", line 142, in print_obj
    print >>out_stream, `obj`
  File "sage_object.pyx", line 154, in sage.structure.sage_object.SageObject.__repr__ (sage/structure/sage_object.c:1463)
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/plot/plot.py", line 1098, in _repr_
    self.show()
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 456, in wrapper
    return func(*args, **kwds)
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/plot/plot.py", line 1750, in show
    self.save(**kwds)
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/misc/decorators.py", line 456, in wrapper
    return func(*args, **kwds)
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2472, in save
    figure = self.matplotlib(**options)
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/sage/plot/plot.py", line 1993, in matplotlib
    leg = subplot.legend(prop=prop, **lopts)
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/matplotlib/axes.py", line 4366, in legend
    handles, labels = self.get_legend_handles_labels()
  File "/sagenb/sage_install/sage-4.7.2/local/lib/python2.6/site-packages/matplotlib/axes.py", line 4208, in get_legend_handles_labels
    label != '' and not label.startswith('_')):
  File "element.pyx", line 330, in sage.structure.element.Element.__getattr__ (sage/structure/element.c:2840)
AttributeError: 'sage.symbolic.expression.Expression' object has no attribute 'startswith'
polira(3,67) 
       

(['para el polinomio', x^3 - 2133*x - 23868, 'las raices son ', [51,
-39, -12]], )

(['para el polinomio', x^3 - 2133*x - 23868, 'las raices son ', [51, -39, -12]], )