TI/Skrypty z zajęć/kz4-6

Z Brain-wiki
Wersja z dnia 11:54, 19 maj 2020 autorstwa Tgub (dyskusja | edycje) (Utworzono nową stronę "<source lang="python"> # -*- coding: utf-8 -*- """ Created on Tue May 19 11:03:13 2020 @author: Tomek """ ''' Zadanie 4. Dopisz w klasie Wielomian (omawianej na zajęc...")
(różn.) ← poprzednia wersja | przejdź do aktualnej wersji (różn.) | następna wersja → (różn.)
# -*- coding: utf-8 -*-
"""
Created on Tue May 19 11:03:13 2020

@author: Tomek
"""

'''
Zadanie 4.
Dopisz w klasie Wielomian (omawianej na zajęciach) metodę "pochodna" 
która zwraca Wielomian odpowiadający pochodnej danego Wielomianu (self).

Zadanie 5.
Dopisz do klasy Wielomian metodę "calka" zwracającą Wielomian 
(nazwijmy go chwilowo w) odpowiadający całce nieoznaczonej z danego Wielomianu. 
Przyjmijmy w(0) = 0.

Zadanie 6.
W klasie Wielomian zaimplementuj operator mnożenia wielomianów.

'''
import numpy as np

class Wielomian():
    def __init__(self,*args):
        self.__a = args
        self.__n = len(args)
        
    def __str__(self):
        if self.__n>0:
            wynik = str(self.__a[0])
            for i in range(1,self.__n):
                wynik += ' + ' + str(self.__a[i]) + '*x^' + str(i)
            return wynik
        return '0'
    
    def __call__(self, x):
        return sum([self.__a[i] * x**i for i in range(self.__n)])
    
    def __add__(self, other):
        wynik = np.zeros(max(self.__n, other.__n))
        
        for i in range(self.__n):
            wynik[i] += self.__a[i]
        
        for i in range(other.__n):
            wynik[i] += other.__a[i]
        
        return Wielomian(*tuple(wynik))
    
    #Zadanie 4
    def pochodna(self):
        return Wielomian(*[i*self.__a[i] for i in range(1,self.__n)])
    
    #Zadanie 5
    def calka(self):
        return Wielomian(0,*[self.__a[i]/(i+1) for i in range(self.__n)])
    
    
    
#    def __mul__(self, other):
#        wynik = np.zeros(self.__n + other.__n - 1)
#        for i in range(self.__n):
#            for j in range(other.__n):
#                #(self.__a[i] * x^i )*(other.__a[j] * x^j ) = self.__a[i]*other.__a[j] * x^(i+j)
#                wynik[i+j] += self.__a[i]*other.__a[j]
#        return Wielomian(*wynik)

    #Zadanie 6 - splot
    def __mul__(self, other):
        #print(np.outer(self.__a,other.__a))
        a = np.outer(self.__a,other.__a)[:,::-1]
        return Wielomian(*[a.trace(i) for i in range(other.__n-1,-self.__n,-1)]) #[2,1,0,-1]]

    
w1 = Wielomian(3,4)
w2 = Wielomian(2,1)

#      (3  ,  4)
# (2)   6     8
# (1)   3     4

#        6 + (3+8)* x^1 + 4 * x^2

print(w1)
print(w2)
print(w2*w1)
    
    
    
    
#w1 = Wielomian(1,20,3,4)

#1 + 2*x^1 + 3*x^2 + 4*x^3 ->(1,20,3,4)
#                            (0,1,2,3)
#pochodna to:
#20*1 + 3*2*x^1 + 4*3*x^2 -> (20,6,12)
#
#calka to:                (a,b,c,d,e)
#                         (0,1,2,3,4)

#                         (b,2*c,3*d,4*e) = (1,20,3,4)
# b=1, c=20/2 d = 3/3, e = 4/4  
# (0,1,10,1,1)                       
#