Skip to main content

Displaying Images in the Terminal

· One min read
ひかり
Main bloger

Introducing the tiv command for displaying images in the terminal.

tiv

It's convenient when you don't want to open a file explorer.

git clone https://github.com/stefanhaustein/TerminalImageViewer.git
cd TerminalImageViewer/src/main/cpp
make -j
sudo make install

Euclidean Algorithm in Bash

· One min read
ひかり
Main bloger
  1. Let C be the remainder when A is divided by B.

  2. Let D be the remainder when B is divided by C.

  3. Let E be the remainder when C is divided by D.

    ...

  4. Let Y be the remainder when X is divided by 0.

    Then Y is the greatest common divisor.

Bash Script

#!/usr/bin/env bash

function gcd(){
test $2 -eq 0 && echo $1 || gcd $2 $(( $1 % $2 ))
}

gcd 1071 1029
# 21

Installing ImageMagick (Ubuntu)

· One min read
ひかり
Main bloger
wget https://download.imagemagick.org/ImageMagick/download/ImageMagick-7.0.11-14.tar.xz

Extract

tar xf ImageMagick-7.0.11-14.tar.xz

Make

sudo apt update
cd ImageMagick-7.0.11-14.tar.xz
./configure
make -j
sudo make install
sudo ldconfig /usr/local/lib

Denoising with a Gaussian Filter (Python / Scipy)

· One min read
ひかり
Main bloger

As an example, let's use a 1[Hz] sine wave as the signal. Add noise generated from random numbers following a normal distribution with a mean of 0 and a standard deviation of 0.5 to the signal.

import numpy as np
import matplotlib.pyplot as plt
from scipy.ndimage import gaussian_filter1d

t = np.arange(1000) / 100
s = np.sin(2*np.pi*t)
noise = np.random.normal(0, 0.5, size=len(t))
x = s + noise

plt.plot(t, x, label="+noise")
plt.plot(t, s, label="signal")
plt.legend(loc=1)
plt.show()

pyplot

Apply a Gaussian filter with a standard deviation of 5. The larger the standard deviation, the smoother the result, but the more it deviates from the original signal.

y = gaussian_filter1d(x, 5)
plt.plot(t, y, label="filtered")
plt.plot(t, s, label="signal")
plt.legend(loc=1)
plt.show()

pyplot

Installing and Configuring Pyenv

· 2 min read
ひかり
Main bloger
  1. Clone Pyenv Clone the Pyenv repository. The recommended directory is ~/ .pyenv.

git clone https://github.com/pyenv/pyenv ~/.pyenv


0. Compile Bash extensions for speed
You can compile Bash extensions for speed.
It will still work correctly if the compilation fails.

```sh
cd ~/.pyenv && src/configure && make -C src
  1. Configuration (bash) Path and other settings.

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile echo 'eval "$(pyenv init --path)"' >> ~/.profile export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" echo -e 'if shopt -q login_shell; then'
'\n export PYENV_ROOT="$HOME/.pyenv"'
'\n export PATH="$PYENV_ROOT/bin:$PATH"'
'\n eval "$(pyenv init --path)"'
'\nfi' >> /.bashrc echo -e 'if [ -z "$BASH_VERSION" ]; then'
'\n export PYENV_ROOT="$HOME/.pyenv"'
'\n export PATH="$PYENV_ROOT/bin:$PATH"'
'\n eval "$(pyenv init --path)"'
'\nfi' >>
/.profile


# Installing Python Environments

## Installing Dependencies
```sh
sudo apt-get update; sudo apt-get install make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblemgma-dev -y

Checking Installable Environments

pyenv install -l

Installing

It takes a long time to compile, so please wait.

CONFIGURE_OPTS= "--enable-shared" pyenv install 3.9.5

Verifying Version

pyenv versions

Switching Version

pyenv global 3.9.5

Dot product with Numo::NArray

· One min read
ひかり
Main bloger
require "numo/narray"

Dot product of real-valued vectors

a = Numo::NArray[4, -1, 2]
b = [2, -2, -1]
c = a.dot b
8

Dot product of complex-valued vectors

a = Numo::NArray[1+1i, 1-1i, -1+1i, -1-1i]
b = [3-4i, 6-2i, 1+2i, 4+3i]
c = a.conj.dot b
(1.0-5.0i)

Dot product of a complex number and itself

d = a.conj.dot a
(8.0+0.0i)

Dot product of matrices

a = Numo::NArray[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
b = [[9, 8, 7], [6, 5, 4], [3, 2, 1]]
c = (a * b).sum(0)
Numo::Int64#shape=[3]
[54, 57, 54]

Calculating dot product by row vector

c = Numo::NArray[(a * b).sum(1)].transpose
Numo::Int64(view)#shape=[3,1]
[[46],
[73],
[46]]

era.js

· 2 min read
ひかり
Main bloger

I created a library for Japanese era names that can be used with JavaScript.

https://himeyama.github.io/era.js/era.js

let date = new Era()
date.getWareki() // "令和X年X月X日" (Today's date)

date = new Era("2020-1-1")
date.getWareki() // "令和2年1月1日"

date.getWareki("西暦") // "2020/01/01"

date.getDateAry() // ["令和", 2, 1, 1]

Era.date2wareki(id) // Convert id to era name

getWareki() Arguments

calshorttypeDisplay
"和暦"true0
1
2
3
4
5
false0
1
2
3
"西暦"true0
1
2
3
4
5
false0
1
2
3
4

[EOL]

Useful JavaScript Tricks

· One min read
ひかり
Main bloger

Looping with indices using for

for([i, e] of ["a", "b", "c"].entries()){
console.log(i, e)
}

Also with forEach

["a", "b", "c"].forEach((e, i) => {
console.log(e, i)
})

Creating an array of length n

const n = 10
let ary = [...Array(n)].map((_, i) => i)
// Or alternatively
ary = Array.from({length: n}).map((_, i)=> i)

Sum of an array

let ary = [1, 2, 3, 4, 5]
ary.reduce((_, v) => _ + v)