summaryrefslogtreecommitdiff
path: root/R/poly.R
blob: 7db046cdbbc99e241082b28f54911aff6e11d492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#' @export
idpoly <- function(A=1,B=1,C=1,D=1,F1=1,ioDelay=0,Ts=1){
  out <- list(A= A,B=B,C=C,D=D,F1=F1,ioDelay = ioDelay,Ts=Ts)
  out$type <- typecheck(out)
  class(out) <- "idpoly"
  return(out)
}

typecheck <- function(x){
  y <- lapply(x[1:5],checkUnity)
  if(y$A){
    out <- if(y$C||y$F1) "oe" else "bj" 
  } else{
    if(y$D && y$F1){
      out <- if(y$C) "arx" else "armax"
    } else{
      out <- "idpoly"
    }
  }
}

checkUnity <- function(x){
  out <- if(length(x)==1 && x==1) TRUE else FALSE
}

#' @export
print.idpoly <- function(x){
  if(x$type=="arx"){
    print_arx(x)
  } else if(x$type=="armax"){
    print_armax(x)
  }
}

print_arx <- function(obj){
  cat("Discrete-time ARX model: A(q^{-1})y[k] = B(q^{-1})u[k] + e[k] \n\n")
  cat("A(q^{-1}) = ")
  for(i in seq_along(obj$A)){
    if(i-1==0){
      cat(obj$A[i])
    } else{
      if(obj$A[i]>0) cat(" + ") else cat("- ")
      
      if(!(abs(obj$A[i])==1)) cat(abs(obj$A[i]))
      cat("q^{-",i-1,"}",sep="")
    }
    cat("\t")
  }
  cat("\n")
  cat("B(q^{-1}) = ")
  for(i in seq_along(obj$B)){
    if(i+obj$ioDelay-1==0){
      cat(obj$B[i])
    } else{
      
      if(!((obj$ioDelay!=0) && (i==1))){
        if(obj$B[i]>0) cat(" + ") else cat("- ")
      } else{
        if(obj$B[i]<0) cat("-")
      }
      
      if(!(abs(obj$B[i])==1)) cat(abs(obj$B[i]))
      cat("q^{-",i+obj$ioDelay-1,"}",sep="")
    }
    cat("\t")
  }
}

print_armax <- function(obj){
  cat("Discrete-time ARX model: A(q^{-1})y[k] = B(q^{-1})u[k] + C(q^{-1})e[k] \n\n")
  cat("A(q^{-1}) = ")
  for(i in seq_along(obj$A)){
    if(i-1==0){
      cat(obj$A[i])
    } else{
      if(obj$A[i]>0) cat(" + ") else cat("- ")
      
      if(!(abs(obj$A[i])==1)) cat(abs(obj$A[i]))
      cat("q^{-",i-1,"}",sep="")
    }
    cat("\t")
  }
  cat("\n")
  cat("B(q^{-1}) = ")
  for(i in seq_along(obj$B)){
    if(i+obj$ioDelay-1==0){
      cat(obj$B[i])
    } else{
      
      if(!((obj$ioDelay!=0) && (i==1))){
        if(obj$B[i]>0) cat(" + ") else cat("- ")
      } else{
        if(obj$B[i]<0) cat("-")
      }
      
      if(!(abs(obj$B[i])==1)) cat(abs(obj$B[i]))
      cat("q^{-",i+obj$ioDelay-1,"}",sep="")
    }
    cat("\t")
  }
  cat("\n")
  cat("C(q^{-1}) = ")
  for(i in seq_along(obj$C)){
    if(i-1==0){
      cat(obj$C[i])
    } else{
      if(obj$C[i]>0) cat(" + ") else cat("- ")
      
      if(!(abs(obj$C[i])==1)) cat(abs(obj$C[i]))
      cat("q^{-",i-1,"}",sep="")
    }
    cat("\t")
  }
}