Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.53 KB | None | 0 0
  1. p = 2 + x⁴ - 3x⁵  # ne va pas s'exécuter
  2.  
  3. p = list(c(-3,5), c(1,4), c(2,0)) # représentation creuse
  4. p = list(c(2,0,0,0,1,-3))  # représentation dense
  5.  
  6. # on utilise la représentation creuse lorsque l'on a à faire à un polynôme avec
  7. # beaucoup de coefficients nuls.
  8. # on utilise la représentation dense lorsque l'on a à faire à un polynôme avec
  9. # beaucoup de coefficients non nul.
  10.  
  11. ##install.packages("polynom")
  12. install.packages("polynom")
  13. library("polynom")
  14. p1 <- polynomial(c(1,-1,1))
  15. p2 <- polynomial(c(-1,0,1))
  16. # affichage
  17. print(p1)
  18. # surcharge des opérateurs
  19. print(p1 + p2)
  20. print(p1 - p2)
  21. # évaluation d'un polynôme en un point
  22. predict(p1,-5:5)
  23. # visualisation d'un polynôme
  24. plot(p1, xlim = c(-10,10))
  25.  
  26. # fonction de détection d'un polynôme nul
  27. is_poly0 <- function(p) length(p) == 0  
  28. is_poly0(p)
  29.  
  30. # fonction de détection du degré d'un monôme
  31. degre <- function(p){          
  32.   if(is_poly0(p)) {return(-Inf)}
  33.   if(is.list(p)) {p <- p[[1]]}
  34.   if(is.vector(p) && length(p) == 2) {return(p[[2]])}
  35.   else{return(NULL)}
  36. }
  37. degre(p[3])
  38. # fonction de détection du coefficient d'un monôme
  39. coeff <- function(p){      
  40.   if(is_poly0(p)) {return(-Inf)}
  41.   if(is.list(p)) {p <- p[[1]]}
  42.   if(is.vector(p) && length(p) == 2) {return(p[[1]])}
  43.   else{return(NULL)}
  44. }
  45.  
  46. # concaténer les listes représentant les 2 polynômes
  47. # trier la nouvelle liste obtenue par degré décroissant
  48. # fusionner les monômes de même degré
  49. # supprimer les monômes dont le coefficient est nul
  50.  
  51. q = list(c(3,5), c(2,3), c(-4,2), c(5,0))
  52. r = list(c(2,6), c(-3,5), c(1,2), c(4,1))
  53.  
  54. poly2str <- function(p){
  55.   for(i in 1:length(p)){
  56.     print(paste(p[[i]][1],"X^",p[[i]][2]))
  57.   }
  58.   i <- i+1
  59. }
  60.  
  61. poly2str <- function(p){list1=""
  62.   for(i in 1:length(p)){
  63.     (list1<-(paste(list1, p[[i]][1],"X^",p[[i]][2],"+")))
  64.   }
  65.   print(intToUtf8(utf8ToInt(list1)[-length(utf8ToInt(list1))]))
  66. }
  67. poly2str(p)
  68.  
  69. mult_ext <- function(p,k){
  70.   acc <- length(p)
  71.   for(i in 1:acc){
  72.     p[[i]][1] <- p[[i]][1]*k
  73.   }
  74.   print(p)
  75. }
  76.  
  77. poly2str(mult_ext(p,-1))
  78.  
  79. d <- c(2, -2, 5, 0, 6)
  80. x <- c(0, -2, 0, 0, -1, 2)
  81. make_poly <- function(x){
  82.   acc <- length(x)
  83.   p <- list()
  84.   y <- 1
  85.   for(i in acc:1){
  86.     if(x[i]!=0){
  87.       p[[y]] <- c(x[i],i-1)
  88.       y <- y+1
  89.     }
  90.   }
  91.   print(p)
  92. }
  93.  
  94. poly2str(make_poly(x))
  95. poly2str(make_poly(d))
  96.  
  97. rand_poly <- function(n,coeff){
  98.   acc <- n
  99.   p <- list()
  100.   y <- 1
  101.   for(i in acc:1){
  102.     p[[y]] <- c(sample(coeff,1,replace = TRUE),i-1)
  103.     y <- y+1
  104.   }
  105.   print(p)
  106. }
  107.  
  108. poly2str(rand_poly(5,6))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement