Skip to main content

Dot product with Numo::NArray

· One min read
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

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

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)

How to Create an Icon (.ico)

· One min read
  1. Create an icon.

    How to create an icon

  2. Prepare seven PNG images of sizes 16, 24, 32, 48, 64, 128, and 256. How to create an icon

  3. Create an icon using the convert command. How to create an icon

convert *.png favicon.ico

Creating an Electron App

· One min read

Creating the working directory

mkdir test-electron-app
cd test-electron-app

Creating package.json

npm init -y

Installing electron

npm i --save-dev electron

Creating index.js

const { app, BrowserWindow } = require("electron")
const path = require("path")

function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js")
}
})

win.loadFile("index.html")
}

app.whenReady().then(() => {
createWindow()

app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})

app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})

Creating index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
<h1>Hello World!</h1>
<p>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
</p>
</body>
</html>

Modifying package.json

Replace the scripts section.

"scripts": {
"start": "electron ."
}

Running the app

npm start

How to make Ruby

· One min read

This is a program that returns 3 as an example.

First, write the source code in C.

// three.c
#include <ruby.h>

static VALUE int_three(void){
return INT2NUM(3);
}

void Init_three(void){
rb_define_singleton_method(rb_cInteger, "three", int_three, 0);
}

Create a script to create a Makefile.

# extcof.rb
require 'mkmf'
create_makfile "three"

Make

$ make

Write a Ruby script to call the created program.

# main.rb
require "./three"
p Integer.three

Run

$ ruby main.rb
3

Creating a DLL and compiling with C

· One min read

Notes on creating a DLL and compiling C

Library

Source file

// gcd.c
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}

Header file

// gcd.h
#ifndef TEST_H
#define TEST_H

int gcd(int a, int b);

#endif

Program

#include <stdio.h>
#include "gcd.h"

int main(void){
printf("%d\n", gcd(24, 36));
}

Compilation

Creating the DLL

gcc gcd.c -shared -o gcd.dll

Compile

gcc main.c -lgcd -L.

unity-terrain-shimabara

· One min read

-- title: Generating Realistic Terrain in Unity

I tried creating realistic terrain in Unity.

Created based on satellite imagery and elevation data from the Geospatial Information Authority of Japan.

Unity Shimabara Peninsula

Next, I'd like to create Kyushu.