<commit_before>---
layout: post
date: 2017-08-28 23:00
title:  "Generating Identity Matrices in Bash"
category: blog
tags: Linux/Bash
---
This post is inspired by another blog post called "[How do I modify a variable in Haskell?](http://www.michaelburge.us/2017/08/15/how-do-i-modify-a-variable-in-haskell.html)" by <NAME>.

It got me thinking about different methods of printing Identity Matrices in Bash (because I am not smart enough to do it in Haskell), and because [Rosetta Code](http://rosettacode.org/wiki/Identity_matrix) was missing a Bash solution.

But first off, what is an Identity Matrix?

Identity Matrixes
-----------------
> A square matrix in which all the main diagonal elements are 1’s and
> all the remaining elements are 0’s is called an Identity Matrix.
> - [web-formulas.com](http://www.web-formulas.com/Math_Formulas/Linear_Algebra_Definition_of_Identity_Matrix.aspx)

And here is an example ([Source](https://en.wikipedia.org/wiki/Identity_matrix)):

<p align="center">
    <img src="https://grayson.sh/assets/images/matrix.svg" alt="Picture of an Identity Matrix'"/>
</p>

Alright, let's get to generating them, or you can <a href="#the-dirty-sed-way">jump to my final solution</a>

Using bc
--------

I usually reach for [`bc`](https://ss64.com/bash/bc.html) if I need to do anything somewhat complex arithmetically in Bash. So let's try that first.

With this script, I am going to try to do things "right" and not code-golf yet. It will also default to 10 if no argument is passed.

You can [try it online](https://tio.run/##JY1NDsIgGETX5RTjJybFpOnPUtqbsIFCLQtBi4sm6tmRpLuZ5L0Zo9Oa8/<KEY> "Bash – Try It Online") or here it is:

```bash
#!/bin/bash
max="${*:-10}";
for i in $(seq "$max"); do<commit_msg>Fix spelling error<commit_after>---
layout: post
date: 2017-08-28 23:00
title:  "Generating Identity Matrices in Bash"
category: blog
tags: Linux/Bash
---
This post is inspired by another blog post called "[How do I modify a variable in Haskell?](http://www.michaelburge.us/2017/08/15/how-do-i-modify-a-variable-in-haskell.html)" by <NAME>.

It got me thinking about different methods of printing Identity Matrices in Bash (because I am not smart enough to do it in Haskell), and because [Rosetta Code](http://rosettacode.org/wiki/Identity_matrix) was missing a Bash solution.

But first off, what is an Identity Matrix?

Identity Matrices
-----------------
> A square matrix in which all the main diagonal elements are 1’s and
> all the remaining elements are 0’s is called an Identity Matrix.
> - [web-formulas.com](http://www.web-formulas.com/Math_Formulas/Linear_Algebra_Definition_of_Identity_Matrix.aspx)

And here is an example ([Source](https://en.wikipedia.org/wiki/Identity_matrix)):

<p align="center">
    <img src="https://grayson.sh/assets/images/matrix.svg" alt="Picture of an Identity Matrix'"/>
</p>

Alright, let's get to generating them, or you can <a href="#the-dirty-sed-way">jump to my final solution</a>

Using bc
--------

I usually reach for [`bc`](https://ss64.com/bash/bc.html) if I need to do anything somewhat complex arithmetically in Bash. So let's try that first.

With this script, I am going to try to do things "right" and not code-golf yet. It will also default to 10 if no argument is passed.

You can [try it online](https://tio.run/##JY1NDsIgGETX5RTjJybFpOnPUtqbsIFCLQtBi4sm6tmRpLuZ5L0Zo9Oa8/nUGh9aUwp76H0i/rnemr77kWRL3ODhA3id3AvEC0BC<KEY>BMUiummQBanRYxxRGDTwQpBiVfWFmXGEXW/3hOfmw3sBXbpjzioVypWNweX8Bw "Bash – Try It Online") or here it is:

```bash
#!/bin/bash
max="${*:-10}";
for i in $(seq "$max"); do
{'fraction_non_alphanumeric': 0.12859938495946324, 'fraction_numerical': 0.023762929829466033, 'mean_word_length': 4.285081240768094, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 8, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1214542', 'n_tokens_mistral': 1282, 'n_tokens_neox': 1183, 'n_words': 415}