Page 2 sur 2

Re: Calcul algèbre Linéaire avec Khicas

Message non luPosté: 31 Mar 2022, 09:11
de parisse
Normalement, il devrait suffire d'editer un fichier numpy.py.tns dans le repertoire de Xcas. Si le fichier n'est pas trouve, c'est un script du source de micropython qui est utilise.
(Je ne suis pas certain que le code ci-dessous soit completement a jour, car le source de micropython utilise des \n a la place de sauts de lignes).
Code: Tout sélectionner
import linalg
import math
class array:
    def __init__(self, a):
        if type(a)==list:
           self.a = a
        else:
           self.a = [a]

    def __add__(self, other):
        B=linalg.add(self.a , other.a)
        return array(B)

    def __sub__(self, other):
        B=linalg.sub(self.a , other.a)
        return array(B)

    def __mul__(self, other):
        if type(self)==array:
            if type(other)==array:
                B=linalg.mul(self.a , other.a)
                return array(B)
            B=linalg.mul(self.a,other)
            return array(B)
        B=linalg.mul(self,other.a)
        return array(B)

    def __rmul__(self, other):
        if type(self)==array:
            if type(other)==array:
                return array(linalg.mul(self.a , other.a))
            return array(linalg.mul(self.a,other))
        return array(linalg.mul(self,other.a))

    def __matmul__(self, other):
        return __mul(self,other)

    def __getitem__(self,key):
        r=(self.a)[key]
        if type(r)==list or type(r)==tuple:
            return array(r)
        return r

    def __setitem__(self, key, value):
        if (type(value)==array):
            (self.a)[key]=value.a
        else:
            (self.a)[key]=value
        return None

    def __len__(self):
        return len(self.a)
   
    def __str__(self):
        return 'array('+str(self.a)+')'
 
    def __repr__(self):
        return 'array('+str(self.a)+')'
 
    def __neg__(self):
        return array(-self.a)

    def __pos__(self):
        return self
   
    def __abs__(self):
        return array(linalg.abs(self.a))

    def __round__(self):
        return array(linalg.apply(round,self.a,linalg.matrix))

    def __trunc__(self):
        return array(linalg.apply(trunc,self.a,linalg.matrix))

    def __floor__(self):
        return array(linalg.apply(floor,self.a,linalg.matrix))

    def __ceil__(self):
        return array(linalg.apply(ceil,self.a,linalg.matrix))

    def T(self):
        return array(linalg.transpose(self.a))
           
def real(x):
    if type(x)==array:
        return array(linalg.re(x.a))
    return x.real

def imag(x):
    if type(x)==array:
        return array(linalg.im(x.a))
    return x.imag

def conj(x):
    if type(x)==array:
        return array(linalg.conj(x.a))
    return linalg.conj(x)

def sin(x):
    if type(x)==array:
        return array(linalg.apply(math.sin,x.a,linalg.matrix))
    return math.sin(x)

def cos(x):
    if type(x)==array:
        return array(linalg.apply(math.cos,x.a,linalg.matrix))
    return math.cos(x)

def tan(x):
    if type(x)==array:
        return array(linalg.apply(math.tan,x.a,linalg.matrix))
    return math.tan(x)

def asin(x):
    if type(x)==array:
        return array(linalg.apply(math.asin,x.a,linalg.matrix))
    return math.asin(x)

def acos(x):
    if type(x)==array:
        return array(linalg.apply(math.acos,x.a,linalg.matrix))
    return math.acos(x)

def atan(x):
    if type(x)==array:
        return array(linalg.apply(math.atan,x.a,linalg.matrix))
    return math.atan(x)

def sinh(x):
    if type(x)==array:
        return array(linalg.apply(math.sinh,x.a,linalg.matrix))
    return math.sinh(x)

def cosh(x):
    if type(x)==array:
        return array(linalg.apply(math.cosh,x.a,linalg.matrix))
    return math.cosh(x)

def tanh(x):
    if type(x)==array:
        return array(linalg.apply(math.tanh,x.a,linalg.matrix))
    return math.tanh(x)

def exp(x):
    if type(x)==array:
        return array(linalg.apply(math.exp,x.a,linalg.matrix))
    return math.exp(x)

def log(x):
    if type(x)==array:
        return array(linalg.apply(math.log,x.a,linalg.matrix))
    return math.log(x)

def size(x):
    if type(x)==array:
        return linalg.size(x.a)
    return linalg.size(x)

def shape(x):
    if type(x)==array:
        return linalg.shape(x.a)

def dot(a,b):
    return a*b

def transpose(a):
    if type(x)==array:
        return array(linalg.transpose(x.a))

def trn(a):
    if type(x)==array:
        return array(linalg.conj(linalg.transpose(x.a)))
    return linalg.conj(linalg.transpose(x.a))

def zeros(n,m=0):
    return array(linalg.zeros(n,m))

def ones(n,m=0):
    return array(linalg.ones(n,m))

def eye(n):
    return array(linalg.eye(n))

def det(x):
    if type(x)==array:
        return linalg.det(x.a)
    return linalg.det(x)

def inv(x):
    if type(x)==array:
        return array(linalg.inv(x.a))
    return linalg.inv(x)

def solve(a,b):
    if type(a)==array:
        if type(b)==array:
            return array(linalg.solve(a.a,b.a))
        return array(linalg.solve(a.a,b))
    if type(b)==array:
        return array(linalg.solve(a,b.a))
    return linalg.solve(a,b)

def eig(a):
    if type(a)==array:
        r=linalg.eig(a.a)
        return array(r[0]),array(r[1])
    return linalg.eig(a)

def linspace(a,b,c):
    return array(linalg.linspace(a,b,c))

def arange(a,b,c=1):
    return array(linalg.arange(a,b,c))

def reshape(a,n,m=0):
    if type(n)==tuple:
        m=n[1]
        n=n[0]
    if type(a)==array:
        return array(linalg.matrix(n,m,a.a))
    return linalg.matrix(n,m,a)

Re: Calcul algèbre Linéaire avec Khicas

Message non luPosté: 31 Mar 2022, 13:42
de rmarion37
Merci pour la réponse,

Je pensais qu'il fallait modifié le fichier source de Micropython pour pouvoir modifier les méthodes de comparaison. Donc si je comprends bien, les méthodes __lt__, __le__, __gt__, __ge__, __eq__ et __ne__ peuvent être rajoutées dans le fichier numpy.py.tns.

En regardant le code source de Numpy, nous pouvons voir :

Code: Tout sélectionner
def __lt__(self: Array, other: Union[int, float, Array], /) -> Array:

def __le__(self: Array, other: Union[int, float, Array], /) -> Array:

def __gt__(self: Array, other: Union[int, float, Array], /) -> Array:

def __ge__(self: Array, other: Union[int, float, Array], /) -> Array:

def __eq__(self: Array, other: Union[int, float, bool, Array], /) -> Array:


Si je comprend bien, le résultat de ces comparaisons est toujours un array quelques soient les types de variables du second membre.

rmarion37

Re: Calcul algèbre Linéaire avec Khicas

Message non luPosté: 31 Mar 2022, 14:46
de parisse
Apparamment oui. Vous avez un lien sur ce source?

Re: Calcul algèbre Linéaire avec Khicas

Message non luPosté: 31 Mar 2022, 16:44
de rmarion37