#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 17 19:11:52 2022
@author: jlebovits
"""
from __future__ import division
import sys
import math
from random import uniform, Random, randrange, randint
#import numpy as np
#import numpy.random as npr
from sympy import *
from sympy.stats import *
# from sympy import Eq, simplify, S, Symbol, Rational, binomial, expand_func
#from sympy.stats import P, E, variance, Die, Normal, DiscreteUniform, sample, Binomial, density
from sympy import Piecewise, log, piecewise_fold, exp, pi, Indexed, S, Symbol, simplify, pprint, Indexed
from sympy import Min, Max, Abs, Interval, oo, sqrt
from sympy.abc import x, y
#print('toto')
[docs]
def deterministes() :
chaine = "deterministes Ok"
return chaine
[docs]
def Namings(name, *args):
""" Fction permettant de créer une fonction dont le nom est donné en argument """
param=str(name)
for p in args:
param=param+'_'+str(p)
return param
#############################################################################################################
#### Début des essais ########
#############################################################################################################
#
#print('Namings("Indi",1,9)','=',Namings('Indi',1,9),'\n')
#
#############################################################################################################
#### Fin des essais ########
#############################################################################################################
[docs]
def Id(x):
"""Pour tout argument x renvoie x"""
return x
#############################################################################################################
#### Début des essais ########
#############################################################################################################
#
#print('Id(',12,')=',type(Id(12)))
#
#############################################################################################################
#### Fin des essais ########
#############################################################################################################
# Fction puissance pième
"""Pour tout argument p renvoie la fction qui élève à la puissance p"""
[docs]
def powers(p):
b=0
def power(x):
t=x**p
return t;
return power
#############################################################################################################
#### Début des essais ########
#############################################################################################################
#
"""Donc powers(3)(x)=x^3"""
#print('Namings("powers",3)','=',Namings('powers',3),'\n')
#print('powers(4)(3)=',powers(4)(3),'\n')
#############################################################################################################
#### Fin des essais ########
#############################################################################################################
def compose(f,x,p):
""" Fction donnant le résultat de f(x)^p i.e. x|-->f(x)^p """
r=f(x)**p
return r
def compo(f,p):
""" Fction i.e. (f,p)-->f(.)^p """
def rho_f_p(x):
r=compose(f,x,p)
return r;
return rho_f_p
#############################################################################################################
#### Début des essais ########
#############################################################################################################
#
#print('compose(sin,5,2)=', compose(sin,5,2))
# print('compose(sin,2)=', compo(sin,2))
# print('compose(sin,2)(5)=', compo(sin,2)(5))
#############################################################################################################
#### Fin des essais ########
#############################################################################################################
################################################################################################################
################################################################################################################
# Fonctions indicatrices
######################################################################################################################################################################################
################################################################################################################
[docs]
def indfction(a,b,x):
"""Pour tout a,b et x donnés renvoie la valeur de la fction indicatrice i1_{[a,b]}(x)"""
e=Piecewise( (1,( (x <= b) and (x >= a) )) , (0, ((x > b) or (x < a))) )
return e;
[docs]
def Indicatfction(a,b):
"""Pour tout a et b donnés, renvoie la fction x|--> i1_{[a,b]}(x)"""
def indi_a_b(x):
f=indfction(a,b,x) #Piecewise( (1,( (x <= b) and (x >= a) )) , (0, ((x > b) or (x < a))) )
return f;
return indi_a_b;
"""Pour tout a et b donnés, renvoie la fction x|--> i1_{[a,b]}(x), via la construction d'un dictionnaire"""
[docs]
def Indicatfctions(a,b):
dico={}
t='Indi'+str(a)+'_'+str(b)
def indi_a_b(x):
f=indfction(a,b,x)#Piecewise( (1,( (x <= b) and (x >= a) )) , (0, ((x > b) or (x < a))) )
return f;
dico[t]=indi_a_b
return dico[t];
#print('Indicatfctions(1,8)(4)=',Indicatfctions(1,8)(4),'\n')
# Pbm, la fction Indicatfction(a,b)(x) pour x symbole ne peut être comprise
# Message d'erreur!
#x = Symbol("x", real=True)
#print('Indicatfction(1,8)(x)=',Indicatfction(1,8)(x),'\n')
#Message d'erruer: raise TypeError("cannot determine truth value of Relational")
# Il ne peut pas évaluer la fction en un symbole !!!!!!!
[docs]
def indfction_v_2(l,a,b,r,x):
"""Pour tout (a,b,x) donnés renvoie la valeur i1_{a,b}(x) , où la nature
des bornes (ouvertes ou fermées) doivent être précisées"""
if l == "[" and r == "]":
e=Piecewise( (1,( (x <= b) and (x >= a) )) , (0, ((x > b) or (x < a))) )
elif l == "[" and r == ')':
e=Piecewise( (1,( (x < b) and (x >= a) )) , (0, ((x >= b) or (x < a))) )
elif l == '(' and r == "]":
e=Piecewise( (1,( (x <= b) and (x > a) )) , (0, ((x > b) or (x <= a))) )
elif l == '(' and r == ')':
e=Piecewise( (1,( (x < b) and (x > a) )) , (0, ((x >= b) or (x <= a))) )
return e;
[docs]
def indi_l_r(l,a,b,r):
"""Pour tout a et b donnés, renvoie la fction x|--> i1_{a,b}(x), où la nature
des bornes (ouvertes ou fermées) doivent être précisées"""
def g_l_a_b_r(x):
f=indfction_v_2(l,a,b,r,x)
return f;
return g_l_a_b_r;
[docs]
def h_l_r(l,a,b,r):
"""Pour tout a et b donnés, renvoie la fction x|--> x.i1_{a,b}(x), où la nature
des bornes (ouvertes ou fermées) doivent être précisées"""
def g_l_a_b_r(x):
f=x*indfction_v_2(l,a,b,r,x)
return f;
return g_l_a_b_r;
#############################################################################################################
#### Début des essais ########
#############################################################################################################
test=float("inf")
a=-test
b=12
l="("
r="]"
# print('indfction_v_2("[",1,3,"]",2)=',indfction_v_2("[",1,3,"]",2))
# print('indfction_v_2("[",1,3,"]",4)=',indfction_v_2("[",1,3,"]",4))
# print('indfction_v_2("[",1,3,"]",4)=',indfction_v_2("[",1,3,"]",-2))
# print('indfction_v_2("[",1,3,"]",4)=',indfction_v_2("[",1,3,"]",test))
# print('indfction_v_2("["-inf,3"]",2)=',indfction_v_2('[',-test,3,']',2))
# print('indfction_v_2("["-inf,3"]",4)=',indfction_v_2("[",-test,3,"]",4))
# print('indfction_v_2("["-inf,inf"]",4)=',indfction_v_2("[",-test,test,"]",4))
# print('indfction_v_2("["-inf,inf"]",4)=',indfction_v_2("[",-test,test,"]",test))
# print('indfction_v_2("(",1,3,")",4)=',indfction_v_2("(",-test,test,")",test))
# print('type(indfction_v_2)=',type(indfction_v_2))
#############################################################################################################
#### Fin des essais ########
#############################################################################################################
[docs]
def indi_l_r_symb(l,alpha,beta,r):
"""Pour tout (alpha,beta,x) donnés renvoie la fction x|--> i1_{a,b}(x) , où la nature
des bornes, notées ici l et r, (ouvertes ou fermées) doivent être précisées"""
"""!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Cette application accepte les r qui sont des symboles et pas juste des réels
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"""
def indifunctionintervalle(t):
#x, y = (Indexed('x', i) for i in (1, 2))
x = Symbol("x", real=True)
if l == "[" and r == "]":
b=Piecewise( (1, x <= beta ), (0, x > beta) )
bprime =Piecewise( (1, x >= alpha ), (0, x < alpha) )
elif l == "[" and r == ')':
b=Piecewise( (1, x < beta ), (0, x >= beta) )
bprime =Piecewise( (1, x >= alpha ), (0, x < alpha) )
elif l == '(' and r == "]":
b=Piecewise( (1, x <= beta ), (0, x > beta) )
bprime =Piecewise( (1, x > alpha ), (0, x <= alpha) )
elif l == '(' and r == ')':
b=Piecewise( (1, x < beta ), (0, x >= beta) )
bprime =Piecewise( (1, x > alpha ), (0, x <= alpha) )
d=b*bprime
c=d.subs(x,t)
return c;
return indifunctionintervalle
[docs]
def indfction_v_1(l,a,b,r,t):
"""Pour tout (a,b,t) donnés renvoie la valeur i1_{a,b}(t) , où la nature
des bornes (ouvertes ou fermées) doivent être précisées"""
#x, y = (Indexed('x', i) for i in (1, 2))
x = Symbol("x", real=True)
e=indi_l_r_symb(l,a,b,r)(x)
c=e.subs(x,t)
return c;
#############################################################################################################
######## Tests des fctions indi_l_r_symb(l, alpha, beta, r) & indfction_v_1 ########
#############################################################################################################
#1- Tests de la fction indi_l_r_symb(l, alpha, beta, r)
# print('indi_l_r_symb("[",1,3,"]")(4)=',indi_l_r_symb("[",1,3,"]")(4),'\n')
# x = Symbol("x", real=True)
# print('indi_l_r_symb("[",1,3,"]")(x)=',indi_l_r_symb("[",1,3,"]")(x),'\n')
# print('indi_l_r_symb("[",1,3,"]")(x).subs(x,2)=',indi_l_r_symb("[",1,3,"]")(x).subs(x,2),'\n')
# z = Symbol("z", real=True)
# print('indi_l_r_symb("[",1,3,"]")(z)=',indi_l_r_symb("[",1,3,"]")(z))
# print('indi_l_r_symb("[",1,3,"]")(z).subs(z,2)=',indi_l_r_symb("[",1,3,"]")(z).subs(z,2),'\n')
#############################################################################################################
#2- Tests de la fction indfction_v_1(l, alpha, beta, r)
#############################################################################################################
# print('indfction_v_1("[",1,3,"]",-1)=',indfction_v_1("[",1,3,"]",-1),'\n')
# print('indfction_v_1("[",1,3,"]",4)=',indfction_v_1("[",1,3,"]",4),'\n')
# print('indfction_v_1("[",1,3,"]",2)=',indfction_v_1("[",1,3,"]",2),'\n')
# print('indfction_v_1("[",1,3,"]",x)=',indfction_v_1("[",1,3,"]",x),'\n')
# print('indfction_v_1("[",1,3,"]",x.subs(x,-1))=',indfction_v_1("[",1,3,"]",x.subs(x,-1)),'\n')
# Et maintenant les deux exemples suivant fonctionnent!!!!!!
# x, y = (Indexed('x', i) for i in (1, 2))
# a=1
# b=2
# pdfVecteindi= ((a*x+b*y)**2+Abs(b)*y**2)*indfction_v_1("[",0,1,"]",x)*indfction_v_1("[",0,2,"]",y)
# print('pdfVecteindi=',pdfVecteindi,'\n')
# pdfVecteindibis= ((a*x+b*y)**2+Abs(b)*y**2)*indi_l_r_symb("[",0,1,"]")(x)*indi_l_r_symb("[",0,2,"]")(y)
# print('pdfVecteindibis=',pdfVecteindibis,'\n')
#############################################################################################################
#### Fin des essais ########
#############################################################################################################
[docs]
def compose(f,x,p):
""" Fction donnant le résultat de f(x)^p i.e. x|-->f(x)^p """
r=f(x)**p
return r
#print('compose(sin,5,2)=', compose(sin,5,2))
[docs]
def compo(f,p):
""" Fction i.e. (f,p)|-->f(.)^p """
def rho_f_p(x):
r=compose(f,x,p)
return r;
return rho_f_p
#############################################################################################################
#### Début des essais ########
#############################################################################################################
#
# print('compose(sin,2)=', compo(sin,2))
# print('compose(sin,2)(5)=', compo(sin,2)(5))
# tau=VarFiniteRV(powers(2),X)
# print('Var(T) =', tau, '\n')
#
#############################################################################################################
#### Fin des essais ########
#############################################################################################################
#print('toto')
# from sympy import Poly, Matrix
# from sympy.abc import x
# mat = Matrix([[Poly(x, x)], [1]])
# print(mat)
# result = mat.T * mat
#print(result)
#simplified = result.applyfunc(lambda p: p.collect(x))
# print(simplified)
# from sympy import ZZ
# from sympy.polys.matrices import DomainMatrix
# A = DomainMatrix([[ZZ(1), ZZ(2)],[ZZ(3), ZZ(4)]], (2, 2), ZZ)
# print('A=',A)
# print('A.charpoly()=',A.charpoly())
from sympy import Matrix
from sympy.polys.matrices import DomainMatrix
# Matrix1 = Matrix([[1, 2],[3, 4]])
# A = DomainMatrix.from_Matrix(Matrix1)
#print('A=',A)