Caching an Inverse of a Matrix Using R

Haszeli Ahmad
2 min readJun 14, 2020

Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of a matrix rather than computing it repeatedly.

This is an example code to cache an inverse of a matrix which then called if:

1. The new matrix computation similar to the previous matrix

2. Matrix is invertible — Read Invertible Matrix

3. The structure and content of the new matrix is equal to previous

The program consists of two functions

  1. A function to cache the matrix and the inverse. It is similar to getter and setter in many programming languages like Java and C.
  2. A function which will be call for the computational process of the matrix

Here are parts of the code:

Setting the matrix
Setting the inverse matrix
Verification on the matrix
Setting the new inverse

Complete source code is reachable at https://github.com/masteramuk/ProgrammingAssignment2

If you like the post, do buy me a coffee please :)

--

--

Haszeli Ahmad

Technologist, Analyst, Engineer and Architect by profession but a photographer by hobby