TI/Skrypty z zajęć/k4
Z Brain-wiki
# -*- coding: utf-8 -*-
"""
Created on Wed May 13 11:17:39 2020
@author: Tomek
"""
import numpy as np
class Liniowa():
def __init__(self, a, b):
self.__a = float(a)
self.__b = float(b)
# @property
# def a(self):
# return self.__a
#
# @property
# def b(self):
# return self.__b
def __str__(self):
return 'Funkcja liniowa '+str(self.__a)+'*x+'+str(self.__b)
def __repr__(self):
return self.__str__()
#print(f1) = print( str(f1) ) = print( f1.__str__() )
#f1(x) = f1.__call__(x)
def __call__(self, x):
return self.__a*x + self.__b
def __add__(self, other):
return Liniowa(self.__a + other.__a, self.__b + other.__b)
def __mul__(self, other):
return Liniowa(self.__a * other.__b + self.__b * other.__a, self.__b*other.__b)
f1 = Liniowa(1,1)
f2 = Liniowa(2,2)
#print(f1)
#print(f2)
#Wielomian(a,b) = a+b*x
#Wielomian(a,b,c) = a+b*x+c*x^2
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):
# if self.__n>0:
# wynik = self.__a[0]
# for i in range(1,self.__n):
# wynik += self.__a[i] * x**i
# return wynik
# return 0
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))
def __mul__(self, other):
# (3 , 4)
# (2) 6 8
# (1) 3 4
# 6 + (3+8)* x^1 + 4 * x^2
pass
w1 = Wielomian(3,4)
w2 = Wielomian(2,1)
print(w1)
print(w2)