Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
ti-nspire / displayShow.py
Last active May 26, 2018 00:34
This program shows a two-digit number, 00-99, on the 5 x 5 LED display of the BBC micro:bit.
from microbit import *
def displayShow():
# Create images for single-digits (0-9), overflow, and underflow.
NUMS = ("00099:00099:00099:00099:00099:", # 0
"00009:00009:00009:00009:00009:", # 1
"00099:00009:00099:00090:00099:", # 2
"00099:00009:00099:00009:00099:", # 3
"00090:00090:00099:00009:00009:", # 4
"00099:00090:00099:00009:00099:", # 5
@ti-nspire
ti-nspire / measurePulseWidth.cpp
Created May 16, 2018 23:28
This program measures the width of a pulse applied to the P0 of the BBC micro:bit. Try to compile the source code by using the Mbed.
#include "MicroBit.h"
MicroBit uBit;
// Define an event handler.
void getPulseWidth(MicroBitEvent e){
printf("%d\n\r", (int)e.timestamp);
}
void printPulseWidth(void){
// Register an event with a message bus.
@ti-nspire
ti-nspire / MinMax.py
Last active March 30, 2018 23:43
ミニマックス近似多項式
# 参考: 三上直樹, (2017), 「知っ得! 軽量sin/cos計算アルゴリズム 第3回 軽量ミニマックス近似式の求め方」, 『Interface(インターフェース)』(CQ出版) 2017年 09 月号, pp.109-111
from matplotlib import pyplot as plt
from scipy.signal import argrelmax
import sympy as sym
import numpy as np
import scipy as sci
class MinMax:
@ti-nspire
ti-nspire / qb_rate.c
Last active March 17, 2018 00:10
QB Rating
#include <stdio.h>
double contain(double var){
double a;
double min = 0 ;
double max = 2.375;
a = (min < var)? var : min;
a = (a < max)? a : max;
return a;
}
@ti-nspire
ti-nspire / epsilon.py
Last active March 31, 2018 23:43
近似多項式との誤差の極値を求める
from scipy.signal import argrelmax
import numpy as np
def epsilon(f, coeffList):
a, b = -1, 1 # 区間
dim = len(coeffList)
f = f
g = lambda x : sum([coeffList[n] * x**n for n in np.arange(dim)])
ε = lambda f, g : f - g
@ti-nspire
ti-nspire / cheby.py
Last active February 26, 2018 23:30
チェビシェフ補間で多項式近似式を求める
# 参考: 『インターフェース』 CQ 出版, 2017 年 9 月号, p.109-111, 三上直樹
# ある函数 f(x) が下のような N 次の多項式 g(x) で表現できると假定し、そこそこ精度のある近似式を求める。
# f(x) ≈ g(x)
# g(x) ≈ c[0] * T[0] + c[1] * T[1] + ... + c[N] * T[N]
# T[N] はチェビシェフ多項式である。
# ここでは例として f(x) = Sin(Pi * x / 2) の近似式を具体的に計算してみる。区間は -1 ≦ x ≦ 1 とする。
# 区間が a ≦ x ≦ b である場合は次式 u(a, b) で変数を変換する。
# u = lambda a, b: (2 * x - (b + a)) / (b - a)
import sympy as sym
import numpy as np
@ti-nspire
ti-nspire / spODE.py
Last active February 4, 2018 00:18
Python による常微分方程式の数値解法 / scipy.integrate.odeint
import scipy as sp
from scipy.integrate import odeint
class spODE:
def __init__(self, funcs, t0, inits, h=1/2**5, numOfDiv=1):
self.funcs = lambda v, t: [func(t, *v) for func in funcs]
self.t0 = t0
self.inits = inits
self.numOfDiv = numOfDiv
self.h = h
@ti-nspire
ti-nspire / extrapo.py
Last active January 22, 2018 23:39
Python による常微分方程式の数値解法 / 補外法
# Python による常微分方程式の数値解法 / 補外法
class Extrapo:
def __init__(self, funcs, t0, inits, h, numOfDiv=1):
self.funcs = funcs
self.t0 = t0
self.inits = inits
self.h = h / numOfDiv
self.numOfDiv = numOfDiv
def __midPoints(self, funcs, t0, inits, H, S=7):
@ti-nspire
ti-nspire / Fehlberg.py
Last active December 9, 2022 09:33
Python による常微分方程式の数値解法 / Fehlberg 法
# Python による常微分方程式の数値解法 / Fehlberg 法
from math import *
class Fehlberg:
def __init__(self, funcs, t0, inits, h, tol=1e-3):
self.funcs = funcs
self.t0 = t0
self.inits = inits
self.h = h
self.numOfDiv = None
@ti-nspire
ti-nspire / Shanks8.py
Last active January 30, 2018 23:28
Python による常微分方程式の数値解法 / Shanks による 12 段 8 次の Runge-Kutta 法
# Python による常微分方程式の数値解法 / Shanks による 12 段 8 次の Runge-Kutta 法
class Shanks8:
def __init__(self, funcs, t0, inits, h, numOfDiv=1):
self.funcs = funcs
self.t0 = t0
self.inits = inits
self.dim = len(funcs)
self.numOfDiv = numOfDiv
self.h = h / self.numOfDiv
self.f = [[None for i in range(self.dim)] for i in range(12)]