Linux(Ubuntu)でScilabによるバッチ処理

複数パラメータを振って計算するときにshellscriptのarrが使えないのが面倒なので。
なんといっても、scilab/matlabはタブ区切り・改行を列・行で認識してくれるのがうれしい

実行方法は:

scilab-cli -nb -nogui -f test.sce -quit

[test.sce]

unix("pwd");
unix("date");

unix("echo test > test.dat"); // リダイレクトもOK
unix("echo "+string(1+3)+" >> test.dat") // ※数値葉文字列に変換すればOK
unix(sprintf("echo %d >> test.dat", 1+3)) // ※sprintfもそのまま使える
unix(sprintf("echo %.1f >> test.dat", 1+3))

data = [
1 2 3
4 5 6
];

for args = data' // dataの行ごとにforループ
    disp(args');
    printf("test1: args = %d, %d, %d\n", args(1), args(2), args(3));
    printf("test2: args = %d, %d, %d\n", args');
    unix(sprintf("echo test3: args = %d, %d, %d\n", args'));
end

// ファイルの実行結果を変数に代入することも可能※unix_gを使う
ret = unix_g("ls *dat");
disp(ret);

ret = unix_g("ls *dat | sed -e ""s/\.dat//g"""); // ""で\"の意
disp(ret);

※実行例:

/home/satonori/
2023年 10月 20日 金曜日 12:34:56 JST

   1.   2.   3.
test1: args = 1, 2, 3
test2: args = 1, 2, 3
test3: args = 1, 2, 3

   4.   5.   6.
test1: args = 4, 5, 6
test2: args = 4, 5, 6
test3: args = 4, 5, 6

  "test.dat"

  "test"

※ファイルの中身

test
4
4
4.0

Linuxマシンの時刻合わせ

以前は「ntpdate ntp.nict.jp」とかだったけどntpdateはデフォルトではなくなったらしい

「chronyc –version」

でコマンドが使えることを確認しつつ

「systemctl status chronyd」

で現在の状態を確認しつつ

systemctl start chronyd
systemctl enable chronyd
chronyc makestep

これでOK

 

GDSファイルからパワポVBに移植するCプログラム※適当に作った

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]){
    int i, j, n;
    char buf[512];
    char fname[512];
    char fname2[512];
    double points[32768][2];
    double bufx, bufy;
    
    if(argc > 1){
        sprintf(fname,  "%s", argv[1]);
        sprintf(fname2, "%s.bas", argv[1]);
    }else{
        sprintf(fname,  "test.gds", argv[1]);
        sprintf(fname2, "test.gds.bas", argv[1]);
    }
    FILE *fp1 = fopen(fname, "r");
    FILE *fp2 = fopen(fname2, "w");
    
    // header
    fprintf(fp2, "' (アスキーコードのGDSから自動生成)\n");
    fprintf(fp2, "' 開発タブのVisualBasic(Alt+F11)から標準モジュールを追加して\n");
    fprintf(fp2, "' 下記スクリプトを入力後、最下部のDrawGDSallを実行(F5)\n");
    
    j = 0;
    while (fscanf(fp1, "%s", buf) != EOF){
        // printf("debug: %s\n",buf);
        if(strcmp(buf, "XY") == 0){
            printf("XY....");
            n = 0;
            while (fscanf(fp1, "%s", &buf) != EOF){
                if(strcmp(buf, "ENDEL") == 0) break;
                points[n][0] = atof(buf);
                fscanf(fp1, "%s", &buf);
                points[n][1] = atof(buf);
                n++;
            }
            printf("OK!(%d)\n", n);
            
            fprintf(fp2, "Sub DrawGDS%d(t As Single)\n", j);
            fprintf(fp2, "    ' 1枚目のスライドのオブジェクトを得る\n");
            fprintf(fp2, "    Dim slide As PowerPoint.slide\n");
            fprintf(fp2, "    Set slide = ActivePresentation.Slides(1)\n");
            fprintf(fp2, "\n");
            fprintf(fp2, "    Dim g%dXY(1 To %d, 1 To 2) As Single\n", j, n+1);
            for(i = 0 ; i < n ; i++){
                fprintf(fp2, "    g%dXY(%d, 1) = %.0f * t\n", j, i+1, points[i][0]);
                fprintf(fp2, "    g%dXY(%d, 2) = %.0f * t\n", j, i+1, points[i][1]);
            }
            fprintf(fp2, "    g%dXY(%d, 1) = %.0f * t\n", j, i+1, points[0][0]);
            fprintf(fp2, "    g%dXY(%d, 2) = %.0f * t\n", j, i+1, points[0][1]);
            fprintf(fp2, "    slide.Shapes.AddPolyline g%dXY\n", j);
            fprintf(fp2, "End Sub\n");
            fprintf(fp2, "\n");
            j++;
        }
    }
    
    fprintf(fp2, "\n");
    
    fprintf(fp2, "' 下記関数にカーソルを合わせて実行(F5)\n");
    fprintf(fp2, "' 各図形は拡大縮小等する前に、図形の結合を行うことを推奨\n");
    fprintf(fp2, "Sub DrawGDSall()\n");
    fprintf(fp2, "    t = 0.01\n");
    for(i = 0 ; i < j ; i++){
        fprintf(fp2, "    DrawGDS%d(t)\n", i);
    }
    fprintf(fp2, "End Sub\n");
    
    return 0;
}

良く忘れるorよく使える英語表現など

, which highlights the practicality of ***.

significantly, considerably

, thereby *ing …

measures(対策)

mitigate(軽減する)

solely(単に、単独で)

fulfill the condition

zoomed-in view of

manage, handle, deal with

categorizeとclassifyはほとんど同じ意味だが、categorizeが不適切な場合があるそう。classifyを使った方が無難

leverages(動詞として使うと ~を活用する、~を効果的に利用する)

canvasテスト(SiO2の屈折率)
https://satonori.jp/canvas/1965.html

データ点を単純な多項式関数で近似(最小二乗法)して、
その時の係数と1階微分/2階微分を算出するScilabのプログラム


clear;

data=[
0.3	0.172876196
0.31	0.154949648
0.32	0.138970801
0.33	0.125236467
0.34	0.113504829
0.35	0.103703211
0.36	0.095478375
0.37	0.088677517
0.38	0.083052247
0.39	0.078446083
0.4	0.074641379
0.41	0.071620587
0.42	0.069189516
0.43	0.067320961
0.44	0.065900801
0.45	0.064934923
0.46	0.064348548
0.47	0.064127407
0.48	0.064260382
0.49	0.06474299
0.5	0.06553752
0.51	0.066732698
0.52	0.068301379
0.53	0.070312216
0.54	0.072818726
0.55	0.075937554
0.56	0.079791231
0.57	0.084628091
0.58	0.090689579
0.59	0.098602723
0.6	0.109086367
];
[N,M] =  size(data); // N行M列

ind1 = 1:2:N; // train
ind2 = 2:2:N; // test

row=2;
data1 = data(ind1,:);
data2 = data(ind2,:);


A1 = [ (data1(:,1)*0+1) data1(:,1)];
b1 = data1(:,row);
A2 = [ (data2(:,1)*0+1) data2(:,1)];
b2 = data2(:,row);

res1=zeros(10,10);
res2=zeros(10,3);
for n=2:10
    A1 = [A1 data1(:,1).^n];
    x = inv(A1' * A1) * A1' * b1;
    disp([n min(svd(A1' * A1))])
    res1(n,1:(n+1)) = x';
    err = (A1*x - b1);
    mse = err' * err;

    A2 = [A2 data2(:,1).^n];
    err2 = (A2*x - b2);
    mse2 = err2 ' * err2; 
    
    res2(n,:) = [n mse mse2];
end

res1=res1';

[minval minind] = min(res2(2:10,3));
order = minind+1;

coef = res1(1:order+1, order);

xx = ones(N,order+1);
for n=2:order+1
    xx(:,n) = data(:,1).^(n-1);
end


aa = zeros(order+1,3);
aa(1:order+1,1) = coef(1:order+1);
aa(1:order  ,2) = diag(1:order) * coef(2:order+1);
aa(1:order-1,3) = diag(1:order-1) * diag(2:order) * coef(3:order+1);

res3=[xx(:,2) (xx*aa)];

fd_w  = mopen("export_mse.txt",'wt');
mfprintf(fd_w, '[interpolate(order:%d)]]\n', order);
for i=1:order+1
    mfprintf(fd_w, '%e\t', coef(i));
end
mfprintf(fd_w, '\nerror=\t%e\t%e\t\n\n', res2(order,3), res2(order,3));
mfprintf(fd_w, 'x\ty\tdy/dx\td2y/dx2\n');
for i=1:N
    mfprintf(fd_w, '%e\t%e\t%e\t%e\n', res3(i,1), res3(i,2), res3(i,3), res3(i,4));
end
mclose(fd_w);

pythonでtsvファイル一括処理

import glob
import pandas

#検索対象
search = '/home/user/*.txt'

#出力ファイル
export = '/home/user/test.dat'
f = open(export, mode='w')

# 指定されたディレクトリ内の全てのファイルを取得
path_list = glob.glob(search)

for fname in path_list:
    data = pandas.read_table(fname)
    a = data.iloc[1,1]
    b = data.iloc[1,2]
    
    res = "%s\t%e\t%e" % (fname,a,b)
    print(res)
    f.write(res+"\n")
    
f.close()

PPT VBによるSベンド描画

開発タブのVisualBasic(Alt+F11)から標準モジュールを追加して
下記スクリプトを入力後、実行(F5)

※参考サイト:
[1] http://whoopsidaisies.hatenablog.com/entry/2014/09/11/124318
[2] https://docs.microsoft.com/ja-jp/office/vba/api/project.shapes.addcurve


Sub DrawCurve()
    ' 1枚目のスライドのオブジェクトを得る
    Dim slide As PowerPoint.slide
    Set slide = ActivePresentation.Slides(1)
       
    l = 100
    s = 10
    w = 10
    
    ' ベジェ曲線を引くための点の座標を配列に格納
    ' ※制御点は2+3n+2 (認識ずれてるかも?)
    Dim points(1 To 13, 1 To 2) As Single
    points(1, 1) = 0
    points(1, 2) = 0
    points(2, 1) = l / 2
    points(2, 2) = 0
    
    points(3, 1) = l / 2
    points(3, 2) = s
    points(4, 1) = l
    points(4, 2) = s
    points(5, 1) = l
    points(5, 2) = s + w
    
    points(6, 1) = l
    points(6, 2) = s
    points(7, 1) = l
    points(7, 2) = s + w
    points(8, 1) = l / 2
    points(8, 2) = s + w
    
    points(9, 1) = l / 2
    points(9, 2) = w
    points(10, 1) = 0
    points(10, 2) = w
    points(11, 1) = 0
    points(11, 2) = 0
    
    points(12, 1) = 0
    points(12, 2) = 0
    points(13, 1) = 0
    points(13, 2) = 0
    
    ' 曲線の追加
    slide.Shapes.AddCurve SafeArrayOfPoints:=points
End Sub

ちなみに、円弧(90度)は


Sub DrawArc90()
' 1枚目のスライドのオブジェクトを得る
    Dim slide As PowerPoint.slide
    Set slide = ActivePresentation.Slides(1)
       
    r = 100
    w = 10
    ri = r - w * 0.5
    ro = r + w * 0.5
    
    kappa = 4 * (Sqr(2) - 1) / 3
    
    
    ' ベジェ曲線を引くための点の座標を配列に格納
    ' ※制御点は2+3n+2 (認識ずれてるかも?)
    Dim points(1 To 13, 1 To 2) As Single
    points(1, 1) = 0
    points(1, 2) = 0
    points(2, 1) = kappa * ro
    points(2, 2) = 0
    
    points(3, 1) = ro
    points(3, 2) = ro - kappa * ro
    points(4, 1) = ro
    points(4, 2) = ro
    points(5, 1) = ri
    points(5, 2) = ro
    
    points(6, 1) = ro
    points(6, 2) = ro
    points(7, 1) = ri
    points(7, 2) = ro
    points(8, 1) = ri
    points(8, 2) = ro - kappa * ri
    
    points(9, 1) = kappa * ri
    points(9, 2) = (ro - ri)
    points(10, 1) = 0
    points(10, 2) = (ro - ri)
    points(11, 1) = 0
    points(11, 2) = 0
    
    points(12, 1) = 0
    points(12, 2) = (ro - ri)
    points(13, 1) = 0
    points(13, 2) = 0
    
    ' 曲線の追加
    slide.Shapes.AddCurve SafeArrayOfPoints:=points
End Sub


DFTマトリクス

DFTマトリクス:
n = 行列の次数
w0 = exp(-%i * 2 * %pi / n);

※i,j : 0スタートとすると
wij = 1/sqrt(n) * w0^(i * j)
※i,j : 1スタートとすると
wij = 1/sqrt(n) * w0^((i-1) * (j-1))

function mat = md(n)
mat = zeros(n,n);
w0 = exp(-%i * 2 * %pi / n);
for i=1:n
for j=1:n
mat(i,j) = 1/sqrt(n) * w0^( (i-1) * (j-1));
end
end
endfunction

追記:FFTを活用しても良いかも?
function mat = md(n)
mat = zeros(n,n)
for i=1:n
mat(:,i) = 1/sqrt(n) * fft(eye(n,n)(:,i))
end
endfunction

バトラーマトリクス

バトラーマトリクス:
n = 行列の次数
w0 = exp(-%i * 2 * %pi / n);

※i,j : 0スタートとすると
wij = 1/sqrt(n) * w0^((i-(n-1)/2) * j)
※i,j : 1スタートとすると
wij = 1/sqrt(n) * w0^(((i-1)-(n-1)/2) * (j-1))

function mat = mb(n)
mat = zeros(n,n);
w0 = exp(-%i * 2 * %pi / n);
for i=1:n
for j=1:n
mat(i,j) = 1/sqrt(n) * w0^(((i-1)-(n-1)/2) * (j-1));
end
end
endfunction