Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- p = 2 + x⁴ - 3x⁵ # ne va pas s'exécuter
- p = list(c(-3,5), c(1,4), c(2,0)) # représentation creuse
- p = list(c(2,0,0,0,1,-3)) # représentation dense
- # on utilise la représentation creuse lorsque l'on a à faire à un polynôme avec
- # beaucoup de coefficients nuls.
- # on utilise la représentation dense lorsque l'on a à faire à un polynôme avec
- # beaucoup de coefficients non nul.
- ##install.packages("polynom")
- install.packages("polynom")
- library("polynom")
- p1 <- polynomial(c(1,-1,1))
- p2 <- polynomial(c(-1,0,1))
- # affichage
- print(p1)
- # surcharge des opérateurs
- print(p1 + p2)
- print(p1 - p2)
- # évaluation d'un polynôme en un point
- predict(p1,-5:5)
- # visualisation d'un polynôme
- plot(p1, xlim = c(-10,10))
- # fonction de détection d'un polynôme nul
- is_poly0 <- function(p) length(p) == 0
- is_poly0(p)
- # fonction de détection du degré d'un monôme
- degre <- function(p){
- if(is_poly0(p)) {return(-Inf)}
- if(is.list(p)) {p <- p[[1]]}
- if(is.vector(p) && length(p) == 2) {return(p[[2]])}
- else{return(NULL)}
- }
- degre(p[3])
- # fonction de détection du coefficient d'un monôme
- coeff <- function(p){
- if(is_poly0(p)) {return(-Inf)}
- if(is.list(p)) {p <- p[[1]]}
- if(is.vector(p) && length(p) == 2) {return(p[[1]])}
- else{return(NULL)}
- }
- # concaténer les listes représentant les 2 polynômes
- # trier la nouvelle liste obtenue par degré décroissant
- # fusionner les monômes de même degré
- # supprimer les monômes dont le coefficient est nul
- q = list(c(3,5), c(2,3), c(-4,2), c(5,0))
- r = list(c(2,6), c(-3,5), c(1,2), c(4,1))
- poly2str <- function(p){
- for(i in 1:length(p)){
- print(paste(p[[i]][1],"X^",p[[i]][2]))
- }
- i <- i+1
- }
- poly2str <- function(p){list1=""
- for(i in 1:length(p)){
- (list1<-(paste(list1, p[[i]][1],"X^",p[[i]][2],"+")))
- }
- print(intToUtf8(utf8ToInt(list1)[-length(utf8ToInt(list1))]))
- }
- poly2str(p)
- mult_ext <- function(p,k){
- acc <- length(p)
- for(i in 1:acc){
- p[[i]][1] <- p[[i]][1]*k
- }
- print(p)
- }
- poly2str(mult_ext(p,-1))
- d <- c(2, -2, 5, 0, 6)
- x <- c(0, -2, 0, 0, -1, 2)
- make_poly <- function(x){
- acc <- length(x)
- p <- list()
- y <- 1
- for(i in acc:1){
- if(x[i]!=0){
- p[[y]] <- c(x[i],i-1)
- y <- y+1
- }
- }
- print(p)
- }
- poly2str(make_poly(x))
- poly2str(make_poly(d))
- rand_poly <- function(n,coeff){
- acc <- n
- p <- list()
- y <- 1
- for(i in acc:1){
- p[[y]] <- c(sample(coeff,1,replace = TRUE),i-1)
- y <- y+1
- }
- print(p)
- }
- poly2str(rand_poly(5,6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement