knitr::opts_chunk$set(echo = TRUE, fig.height = 7,fig.width = 8)
options() # printing all current options## $add.smooth
## [1] TRUE
##
## $browserNLdisabled
## [1] FALSE
##
## $CBoundsCheck
## [1] FALSE
##
## $check.bounds
## [1] FALSE
##
## $citation.bibtex.max
## [1] 1
##
## $continue
## [1] "+ "
##
## $contrasts
## unordered ordered
## "contr.treatment" "contr.poly"
##
## $defaultPackages
## [1] "datasets" "utils" "grDevices" "graphics" "stats" "methods"
##
## $demo.ask
## [1] "default"
##
## $deparse.cutoff
## [1] 60
##
## $device
## function (width = 7, height = 7, ...)
## {
## grDevices::pdf(NULL, width, height, ...)
## }
## <bytecode: 0x0000000014294d78>
## <environment: namespace:knitr>
##
## $device.ask.default
## [1] FALSE
##
## $digits
## [1] 7
##
## $echo
## [1] FALSE
##
## $editor
## [1] "notepad"
##
## $encoding
## [1] "native.enc"
##
## $example.ask
## [1] "default"
##
## $expressions
## [1] 5000
##
## $help.search.types
## [1] "vignette" "demo" "help"
##
## $help.try.all.packages
## [1] FALSE
##
## $help_type
## [1] "text"
##
## $htmltools.preserve.raw
## [1] TRUE
##
## $HTTPUserAgent
## [1] "R (4.1.2 x86_64-w64-mingw32 x86_64 mingw32)"
##
## $install.packages.compile.from.source
## [1] "interactive"
##
## $internet.info
## [1] 2
##
## $keep.parse.data
## [1] TRUE
##
## $keep.parse.data.pkgs
## [1] FALSE
##
## $keep.source
## [1] FALSE
##
## $keep.source.pkgs
## [1] FALSE
##
## $knitr.in.progress
## [1] TRUE
##
## $locatorBell
## [1] TRUE
##
## $mailer
## [1] "mailto"
##
## $matprod
## [1] "default"
##
## $max.print
## [1] 99999
##
## $menu.graphics
## [1] TRUE
##
## $na.action
## [1] "na.omit"
##
## $nwarnings
## [1] 50
##
## $OutDec
## [1] "."
##
## $pager
## [1] "internal"
##
## $papersize
## [1] "a4"
##
## $PCRE_limit_recursion
## [1] NA
##
## $PCRE_study
## [1] FALSE
##
## $PCRE_use_JIT
## [1] TRUE
##
## $pdfviewer
## [1] "C:/PROGRA~1/R/R-41~1.2/bin/x64/open.exe"
##
## $pkgType
## [1] "both"
##
## $prompt
## [1] "> "
##
## $repos
## CRAN
## "@CRAN@"
##
## $scipen
## [1] 0
##
## $show.coef.Pvalues
## [1] TRUE
##
## $show.error.messages
## [1] TRUE
##
## $show.signif.stars
## [1] TRUE
##
## $showErrorCalls
## [1] TRUE
##
## $str
## $str$strict.width
## [1] "no"
##
## $str$digits.d
## [1] 3
##
## $str$vec.len
## [1] 4
##
## $str$list.len
## [1] 99
##
## $str$deparse.lines
## NULL
##
## $str$drop.deparse.attr
## [1] TRUE
##
## $str$formatNum
## function (x, ...)
## format(x, trim = TRUE, drop0trailing = TRUE, ...)
## <environment: 0x000000001514f898>
##
##
## $str.dendrogram.last
## [1] "`"
##
## $stringsAsFactors
## [1] FALSE
##
## $tikzMetricsDictionary
## [1] "LAB1_ALJ4_2022-tikzDictionary"
##
## $timeout
## [1] 60
##
## $try.outFile
## A connection with
## description "output"
## class "textConnection"
## mode "wr"
## text "text"
## opened "opened"
## can read "no"
## can write "yes"
##
## $ts.eps
## [1] 1e-05
##
## $ts.S.compat
## [1] FALSE
##
## $unzip
## [1] "internal"
##
## $useFancyQuotes
## [1] FALSE
##
## $verbose
## [1] FALSE
##
## $warn
## [1] 0
##
## $warning.length
## [1] 1000
##
## $width
## [1] 80
##
## $windowsTimeouts
## [1] 100 500
options(digits=5) # number of digits to display
# LIB
library("xtable")
# R HASIBERRIENTZATAriketa honetan landuko dugu gelan ikasitako hainbat kontzeptu:
Oinarrizko matrizeak LU faktorizazioa
Gauss-Jordan algoritmoa
Eta birpasatuko ditugu R programaren hainbat aginduak:
function, library, matrix, dim, ncol, nrow, det, t, rank, source, solve, cbind, rbind, xtable, print, MATRIZE ERAGIKETAK: +, %*%, ….
Birpasatu oinarrizko matrizeen definizioak eta sortu funtzio bat eragiketa bakoitza definitzeko.
Definizioak idatziko dira ondoko script fitxeroan: OinarrzikoM.R
# KONPILAZIOA: Kode zati hau exekutatu ahal izateko aldatu behar da
# ondoko parametroaren balioa: eval = TRUE
# fitxeroan dagoen kodea memorian kargatuko da:
source("OinarrizkoM.R")Aurkitu \(A\) matrizearen LU faktorizazioa:
\[ A= \left( \begin{array}{rrrr} 0 & 2 & 2 & 0\\ 1 & 4 & 0 & 2\\ 1 & 0 & 0 & 2 \end{array}\right) \]
# KONPILAZIOA: Kode zati hau exekutatu ahal izateko aldatu behar da ondoko parametroaren balioa: eval = TRUE
#
# 1. Definitu A matrizea R kodearekin
A <- matrix(c(0,2,2,0, 1,4,0,2, 1,0,0,2), ncol = 4, nrow = 3, byrow = TRUE)
# Kalkulatu matrizearen dimentsioak
A.dim <- dim(A)# KONPILAZIOA: Kode zati hau exekutatu ahal izateko aldatu behar da
# ondoko parametroaren balioa: eval = TRUE
#1. Pausua
# Notazioa:
# Pausu bakoitzaren matrizearen izenak: A, A1, A2, ... U(azkena)
# Oinarrizko eragiketen matrizeak idatziko dira OinarrzikoM.R fitxategian definitzen diren moduan.# KONPILAZIOA: Kode zati hau exekutatu ahal izateko aldatu behar da
# ondoko parametroaren balioa: eval = TRUE
A.dim <- dim(A)
(B <- Pme(A.dim[1], 1, 3))## [,1] [,2] [,3]
## [1,] 0 0 1
## [2,] 0 1 0
## [3,] 1 0 0
B## [,1] [,2] [,3]
## [1,] 0 0 1
## [2,] 0 1 0
## [3,] 1 0 0
(A1 <- B %*% A)## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 2
## [2,] 1 4 0 2
## [3,] 0 2 2 0
B <- Pme(A.dim[1], 1, 3)
(A1 <- B %*% A)## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 2
## [2,] 1 4 0 2
## [3,] 0 2 2 0
(C <- Eme(A.dim[1], 2, 1, -1))## [,1] [,2] [,3]
## [1,] 1 0 0
## [2,] -1 1 0
## [3,] 0 0 1
(A2 <- C %*%A1)## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 2
## [2,] 0 4 0 0
## [3,] 0 2 2 0
(D <- Eme(A.dim[1], 3, 2, -1/2))## [,1] [,2] [,3]
## [1,] 1 0.0 0
## [2,] 0 1.0 0
## [3,] 0 -0.5 1
(A3 <- D%*%A2)## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 2
## [2,] 0 4 0 0
## [3,] 0 0 2 0
U <- A3
(L_alderantzizkoa <- D%*%C)## [,1] [,2] [,3]
## [1,] 1.0 0.0 0
## [2,] -1.0 1.0 0
## [3,] 0.5 -0.5 1
L <- solve(L_alderantzizkoa)
L## [,1] [,2] [,3]
## [1,] 1 0.0 0
## [2,] 1 1.0 0
## [3,] 0 0.5 1
#determinantea
det(L)## [1] 1
# KONPILAZIOA: Kode zati hau exekutatu ahal izateko aldatu behar da
# ondoko parametroaren balioa: eval = TRUE
LU <- L%*%U
LU ## [,1] [,2] [,3] [,4]
## [1,] 1 0 0 2
## [2,] 1 4 0 2
## [3,] 0 2 2 0
PA <- B %*% A
all.equal(LU, PA)## [1] TRUE
Aurkitu \(A\) matrizearen alderantzizkoa Gauss-Jordan algorimoa erabiliz.
\[A= \begin{pmatrix} 2 & 4 & 0\\ 0 & 2 & 1\\ 3 & 0 & 2 \end{pmatrix}\]
Gogoratu erabilitako matrize guztiak definitu behar dituzula.
# KONPILAZIOA: Kode zati hau exekutatu ahal izateko aldatu behar da
# ondoko parametroaren balioa: eval = TRUE
# Definitu A matrizea eta kalkulatu bere alderantzizkoa
A <- matrix(c(2,4,0, 0,2,1, 3,0,2), ncol = 3, nrow = 3, byrow = TRUE)
At<-t(A)
At## [,1] [,2] [,3]
## [1,] 2 0 3
## [2,] 4 2 0
## [3,] 0 1 2
U matrizeko LaTeX kodea idatziko da ondoko kodea exekutatzean.
# R kodea matrize bat idazteko LaTeX-en
# https://stackoverflow.com/questions/45591286/for-r-markdown-how-do-i-display-a-matrix-from-r-variable
write_matex2 <- function(x) {
begin <- "\\begin{pmatrix}"
end <- "\\end{pmatrix}"
X <-
apply(x, 1, function(x) {
paste(
paste(x, collapse = "&"),
"\\\\"
)
})
paste(c(begin, X, end), collapse = "")
}\(U\) matrizea idatzi: \(U = \begin{pmatrix}1&0&0&2 \\0&4&0&0 \\0&0&2&0 \\\end{pmatrix}\)