Open50

misc

ピン留めされたアイテム
ultimatileultimatile

分類に困った小ネタ集です.
shell周りが多いと思います.

ultimatileultimatile

CPU温度の監視(macOS)

terminal
sudo powermetrics -s smc | grep "CPU die temperature"
出力
CPU die temperature: 78.96 C

reference

comment

旧version

最初は

sudo powermetrics | grep -e "CPU die temperature"

にしてたけどproc_pidpath ***** failed(0)が出てきてウザかったので上記のcommandに変更.

awkできない

よくわかんないけど

sudo powermetrics -s smc | grep "CPU die temperature"| awk '{print $4}'

とかやって温度の数値だけ抜き出そうとしても何も出てこない.
最初のgrepはできているのに...?

#これはいける
echo "CPU die temperature: 65.67 C"| awk '{print $4}'
65.67
ultimatileultimatile

terminalの表示名を変更する

shell変数のPS1を変更するとterminalで常に表示されているuser名とかcurrrent directoryとかが表示されているのが変更できる.

default
user名@PC名 current directory %
export PS1="%C "'%% '

こうすると

表示
current directory % 

こうなる.

reference

https://eng.shibuya24.info/entry/zsh_remove_pcname_and_username

comment

zshで%をescapeするには%%.
プロンプトと言うらしい.

ultimatileultimatile

shellで小数の桁指定

(ba)shだとたぶんpythonとか使うのが一番簡単(なんだそれ)

terminal
python -c "print(f'{1.22/2:.2f}')"
0.61

zshならprintfでOK

terminal
printf "%.2f\n" $((1.22/2))
0.61

comment

bashの小数演算がめんどくさすぎる.
python version 3.6以上で使えるf-stringというのを用いている.
1.22/2.2fで表示してくださいという意味になっている.

TODO

  • formatの記法
  • shell変数${A}を含む場合,Pythonのplace holderにつかう括弧と衝突するのでなんとかしないといけない
ultimatileultimatile

小数の割り算を計算をしてから整数に変換

echo "$A/$B" | bc -l | xargs printf "%.0f"

scaleがややこしいのでbc -lにしてscale=20になるようにしている
整数にするのは"%.0f"が楽

ultimatileultimatile

画像とかの変換系

探せばすぐ出てくるけど探すのが面倒で...

pdf→png

terminal
 brew install poppler
terminal
pdftoppm {input.pdf} {output.file} -png

{output.file}は拡張子なし
https://www.cyberciti.biz/faq/how-to-convert-pdf-to-image-on-linux-command-line/

dpiの変更

-r  {value} # DPI (default: 150)
-rx {value} # 横方向のDPI (default: 150)
-ry {value} # 縦方向のDPI (default: 150)

他に使いそうなoption

-singlefile #単一page pdfを変換するときに指定すると出力されるfile名に番号がつかない

svg→png

terminal
inkscape -w 1024 -h 1024 input.svg -o output.png

https://stackoverflow.com/questions/9853325/how-to-convert-a-svg-to-a-png-with-imagemagick

ultimatileultimatile

Markdownでacuteとか

HTML用に特殊文字が用意されている.

acute

Poincaréとか
&{}acute;で{}にalphabetが入る.
Poincaréのように書く.
あるやつとないやつがある
éみたいに番号で直接指定も可能

umlaut

Schrödingerとか
&{}uml;で{}にaiueoyAIUEOYが入る.
Schrödingerのように書く.

double acute

ő

ø(空集合記号∅ではない)

o slashとかいうらしい.
Møllerとか
ø
ちなみに空集合記号∅は∅

reference

https://t.co/f4TjCGwEiY
https://www.adfreak.de/en/blog/html-umlauts-and-special-characters-entities-and-unicodes-list/

comment

番号の方はUnicodeで分かりやすい方はHTML用に用意されているものっぽい.

ultimatileultimatile

VESTAの使いかた

  • 水素結合はbondからOH結合から消せる
  • 一部の原子だけ表示するにはboundaryから
  • abc軸を表す矢印は消せる
ultimatileultimatile

空行を含めて全ての行頭に>を1つだけ挿入する

sed -i -e 's/^$/> &/' -e 's/^\([^>]\)/> \1/' your_file.text
ultimatileultimatile

Hermite行列の指数函数がunitaryになるとき係数は純虚数

Hermite行列\hat{A}に対して\mathrm{e}^{t\hat{A}}を考える(t\in\mathbb{C}).
このとき\mathrm{e}^{t\hat{A}}がunitaryであることとtが純虚数であることは同値.

まず, Herimite行列はunitary行列で対角化可能なので, \hat{A}を対角化するunitary行列\hat{V}, 実数成分を持つ対角行列\hat{D}を用いて\hat{A}=\hat{V}\hat{D}\hat{V}^\daggerと対角化できる.
この表式を用いると

\begin{aligned} \mathrm{e}^{t\hat{A}} =\sum_{k=0}^\infty\frac{t^k}{k!}\hat{A}^k &=\sum_{k=0}^\infty\frac{t^k}{k!}\hat{V}\hat{D}^k\hat{V}^\dagger\ (\because \hat{V}\hat{V}^\dagger=\hat{1})\\ &=\hat{V}\mathrm{e}^{t\hat{D}}\hat{V}^\dagger \end{aligned}

となる.

次に, \left(\mathrm{e}^{t\hat{A}}\right)^\daggerを計算する.
\hat{D}は実数成分を持つことから\hat{D}^\dagger=\hat{D}となることに注意すると

\left(\mathrm{e}^{t\hat{A}}\right)^\dagger=\hat{V}\mathrm{e}^{t^\ast\hat{D}}\hat{V}^\dagger

となる.

最後に\mathrm{e}^{t\hat{A}}\left(\mathrm{e}^{t\hat{A}}\right)^\daggerを計算する.
t\hat{D}t^\ast\hat{D}は交換することから\mathrm{e}^{t\hat{D}}\mathrm{e}^{t^\ast\hat{D}}=\mathrm{e}^{(t+t^\ast)\hat{D}}
となることに注意すると

\mathrm{e}^{t\hat{A}}\left(\mathrm{e}^{t\hat{A}}\right)^\dagger=\hat{V}\mathrm{e}^{(t+t^\ast)\hat{D}}\hat{V}^\dagger

となる.

\mathrm{e}^{t\hat{A}}がunitaryになるためには\mathrm{e}^{t\hat{A}}\left(\mathrm{e}^{t\hat{A}}\right)^\dagger=\hat{1}とならなければならない.
そのためには\mathrm{e}^{(t+t^\ast)\hat{D}}=\hat{1}, すなわち(t+t^\ast)\hat{D}=\hat{O}となればよい.
\hat{D}は一般に零行列\hat{O}ではないのでt+t^\ast=0にならなければならない.
これはtが純虚数を意味する.

ultimatileultimatile

これらの記号を含むpasswordの生成

pwgen 12 -cnysB1 -N1000| grep -E '^[a-zA-Z0-9\-\.\@\+\$\#\!\/\_]{12}$'

ultimatileultimatile

homebrewでinstallしたときにPATHを解決しない場合がありcompile時にinclude/link optionを指定する必要がある場合がある.

  • brewの用語でkeg-onlyという
  • 例えば, LAPACK/BLASをinstallしたときにAccelerate frameworkと衝突しないようにするために発生する
  • brew info openblasとかやると見れる

OpenBLAS

LDFLAGS:  -L/opt/homebrew/opt/openblas/lib
CPPFLAGS: -I/opt/homebrew/opt/openblas/include
  • 渡すべきPATHは環境によって異なる可能性があるためPATHは適宜書き直す必要がある.
  • とにかくhomebrewの下にあるopenblas/libopenblas/includeの場所を渡せば良い.
ultimatileultimatile
For pkg-config to find openblas you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/openblas/lib/pkgconfig"
ultimatileultimatile

LAPACK

  export LDFLAGS="-L/opt/homebrew/opt/lapack/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/lapack/include"

For pkg-config to find lapack you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/lapack/lib/pkgconfig"
ultimatileultimatile

OpenMP

libomp is keg-only, which means it was not symlinked into /opt/homebrew,
because it can override GCC headers and result in broken builds.

For compilers to find libomp you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/libomp/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/libomp/include"
ultimatileultimatile

LLVM Clang

If you need to have llvm first in your PATH, run:

  echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.zshrc

For compilers to find llvm you may need to set:

  export LDFLAGS="-L/usr/local/opt/llvm/lib"
  export CPPFLAGS="-I/usr/local/opt/llvm/include"
ultimatileultimatile

Modules

To activate modules, add the following at the end of your .zshrc:

  source /opt/homebrew/opt/modules/init/zsh

You will also need to restart your terminal for this change to take effect.
ultimatileultimatile

brewで入るHPC用soft

以下全てbrew install hogeの形

# build
cmake
gcc # GNU C compiler
llvm # LLVM clang
ninja

# BLAS/LAPACK
lapack
openblas
scalapack

# MPI
open-mpi
mpich

# OpenMP
libomp

# Misc
juliaup # julia
rustup-init # rust

# package maneger
conan 
spack
ultimatileultimatile

PostgreSQL

To start postgresql@14 now and restart at login:

brew services start postgresql@14

Or, if you don't want/need a background service you can just run:

/opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14
ultimatileultimatile

buildが終わったら通知する(macOS)

https://qiita.com/s2mr/items/ed10bca41ab3ee88f3db
画面横に通知カードを出す(トースト通知というらしい)
こういうやつ

BUILD_COMMAND && osascript -e 'display notification "Build finished" with title "Build"'

通知をoffにしていると表示されないのでその場合はpopup windowにする
こういうやつ

BUILD_COMMAND && osascript -e 'tell app "System Events" to display dialog "Build finished"'
ultimatileultimatile

intel macで使っていたHomebrewをM1 macでそのまま使うと不具合が出る

自分はlimaのinstallでこけた.
おそらくx86_64 or AArch64がどうとかキレられたのはこれのせいな気がする

1. Homebrewをuninstallする

NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

2. Homebrewを再installする

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

最新のcommandは以下で確認

参考文献

https://github.com/Homebrew/install

ultimatileultimatile

スパコンにninjaを入れる

前提

  • Git
  • CMake
  • sudo不可

やること

sudoなければsource build

build command

git clone https://github.com/ninja-build/ninja.git
cd ninja
cmake -Bbuild-cmake -DCMAKE_INSTALL_PREFIX=$HOME
cmake --build build-cmake
cmake --install ./build-cmake

補足

  • build-cmakeはbuild用のdirectory名なので変更して良い
  • -DCMAKE_INSTALL_PREFIX=$HOME$HOME(=loginしたときに最初にいるdirectory)にbinaryを置く設定. ~/bin/ninjaのようにinstallされる. したがって$HOMEの部分は自分がinstallしたい場所に変更して良い.

参考文献

https://qiita.com/koyukitukimino/items/4f81a159a1f63469a708

ultimatileultimatile

クリップボードに何か処理してクリップボードにコピー

pbpaste | なんか処理する | pbcopy
ultimatileultimatile

Shell: 標準出力の最初のn行を飛ばす

tail -n +[n+1]

[n+1]は実際は数値なので注意(e.g. 最初の1行を飛ばす:tail -n +2)

ultimatileultimatile

known_hostsにある鍵の消し方

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! とか出たときにknown_hostsにある鍵を消さないといけない.一応手でも消せるが

ssh-keygen -R [host name or IP address]

で消せる.

ultimatileultimatile

uname -marchの違い (macOS)

通常のterminal
$ uname -m
arm64
$ arch
arm64
Rosetta terminal
$ uname -m
x86_64
$ arch
i386
ultimatileultimatile

Z shellで読み込まれるprofile/rc filleとその順番

各dotfileにechoを入れて検証(macOSで試しただけ)

login shell (zsh --login)

.zshenv.zprofile.zshrc

非login shel l(zsh)

.zshenv.zshrc

zsh --login --norcs or zsh --norcs

--norcsを付けるとlogin shellか非login shellかに関わらず何も読み込まれない

他に.zlogin.zlogoutなどがあるが自分は使用していないのでパス.

ultimatileultimatile

水平に出力を連結~hcat的な: paste

ultimatileultimatile

fileから特定の列を最初の列に新しく追加して水平方向に連結

awk '{print $4}' list[1-4].dat | paste <(for i in $(seq -w 0.0 0.1 1.0); do echo "$i" ;done) - - - -
ultimatileultimatile

A0 posterをA4に分割して印刷する用のpdfを作成

brew install mupdf 
mutool poster -x 4 -y 4 input.pdf output.pdf
ultimatileultimatile

便利そうなcommand line tool

  • hgrep: Grep with human-friendly search results
  • hyperfine: A command-line benchmarking tool
  • pdfgrep: a commandline utility to search text in PDF files
  • cheat: cheat allows you to create and view interactive cheatsheets on the command-line. It was designed to help remind *nix system administrators of options for commands that they use frequently, but not frequently enough to remember.
ultimatileultimatile

fish移行memo

大体ここに書いてある
https://natsukium.github.io/fish-docs-jp/index.html

.zshrc & .zprofile ~/.config/fish/config.fish

source ~/.config/fish/config.fish

変数とexport

.venv

Homebrew

同じ
eval (/opt/homebrew/bin/brew shellenv)

ultimatileultimatile

Notion, Obsidian, Logseqの比較

Notion

  • 良いところ
    • みんな使ってる
  • 悪いところ
    • 重い
    • localなmarkdown fileが生成されない(他のtoolはhoge.mdが作成される)
    • 日本語の後に$$を入力して数式modeにしようとしても数式がrenderingされない($$の前にspaceが必要)

Obsidian

Logseq