Skip to content

Add files via upload #5826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Programming Assignment2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## makeCacheMatrix : crée un objet "matrice" capable de mettre en cache son inverse
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL # initialisation du cache pour l'inverse

# Définir la matrice
set <- function(y) {
x <<- y
inv <<- NULL # réinitialiser le cache si la matrice change
}

# Obtenir la matrice
get <- function() x

# Mettre en cache l'inverse
setInverse <- function(inverse) inv <<- inverse

# Obtenir l'inverse mis en cache
getInverse <- function() inv

# Retourner la liste de toutes les fonctions
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}


## cacheSolve : calcule l'inverse de la matrice "spéciale"
## Si l'inverse est déjà en cache, le retourne directement
cacheSolve <- function(x, ...) {
inv <- x$getInverse()

if(!is.null(inv)) {
message("getting cached data")
return(inv)
}

# Si pas en cache, calculer l'inverse
mat <- x$get()
inv <- solve(mat, ...) # calcul de l'inverse
x$setInverse(inv) # mise en cache
inv # retourner l'inverse
}