๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

IN DEPTH CAKE/Coding-WIKI

(10)
[Hello! c++] constexpr const char *์— ๋Œ€ํ•ด์„œ ์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ค๋Š˜ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃฐ ๋‚ด์šฉ์€ constexpr const char * ์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ์ž…๋‹ˆ๋‹ค. ์ด ์š”์ƒํ•˜๊ฒŒ ์ƒ๊ธด ๋ฌธ๋ฒ•์ด ๋งž๋Š” ํ‘œํ˜„์ธ์ง€ ์ด์•ผ๊ธฐํ•ด๋ณด๋„๋ก ํ•˜์ฃ . ์šฐ์„ , constexpr์ด ๋ญ”๊ฐ€? constexpr๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ const์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, const ๋ณด๋‹ค ์ข์€ ๊ฐœ๋…์œผ๋กœ ์—ฌ๊ฒจ์ง‘๋‹ˆ๋‹ค. ์ผ๋‹จ ์•Œ์•„๋‘˜ ์ ์€ constexpr๋ฅผ ์“ฐ๋ฉด ๋’ค์— ์˜ค๋Š” ๋‚ด์šฉ์ด const๋ผ๊ณ  ์„ ์–ธํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ์ ์—์„œ const์™€ ๋‹ค๋ฅผ ๊ฒŒ ์—†์ฃ . ๊ทธ๋Ÿฌ๋ฉด ๋‘ ๊ฐœ๊ฐ€ ๊ฐ™์€ ๊ฑด๊ฐ€์š”? ์•„๋‹ˆ์˜ค, constexpr๋Š” const๋กœ ๋Œ€์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, const๋Š” constexpr๋กœ ํ•ญ์ƒ ๋Œ€์ฒด๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๋ฌด์Šจ๋ง์ด๋ƒ๋ฉด, constexpr๋Š” const์ฒ˜๋Ÿผ ๋’ค์— ์˜ค๋Š” ์‹์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” "constant (์ƒ์ˆ˜)"์ž„์„ ์˜๋ฏธํ•œ๋‹ค๋Š”..
[์•Œ๊ณ ๋ฆฌ์ฆ˜] ์ด์ง„ ํƒ์ƒ‰ (Binary Search) - ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ๊นŒ์ง€ ์‚ดํŽด๋ณด๊ธฐ ๋ชฉ์ฐจ ๐ŸŒ  ๊ฐœ์š” ์ด์ง„ ํƒ์ƒ‰ ์„ค๋ช… ์ด์ง„ ํƒ์ƒ‰ template Array ๋‚ด์— ์ค‘๋ณต๋œ ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ Array ๋‚ด์— ์ค‘๋ณต๋œ ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์ฃผ์š” ํŒจํ„ด ์„ค๋ช…์€ ๋‹ค์Œ์—... ๊ฐœ์š” ์ด์ง„ ํƒ์ƒ‰์€ ํฌ๊ธฐ๊ฐ€ $n$ ์ธ search space์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, worst case์—์„œ์˜ ๋ณต์žก๋„๊ฐ€ $O(\log n)$ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์ด์ง„ ํƒ์ƒ‰์€ ์ •๋ ฌ๋˜์–ด ์žˆ๋Š” ํƒ์ƒ‰ ๊ณต๊ฐ„์— ๋Œ€ํ•ด์„œ ์ ์šฉ๋œ๋‹ค. ์ด์ง„ ํƒ์ƒ‰์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋“ค ์ค‘ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด์ž๋ฉด... ์ •๋ ฌ๋œ ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง€๊ณ  ํŠน์ • ๊ฐ’์ด ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•  ์œ„์น˜๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๊ฒฝ์šฐ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์— ๋Œ€ํ•ด์„œ ํŠน์ • ๊ฐ’ ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ์ž‘์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐœ์ˆ˜๋ฅผ ๋ฌผ์–ด๋ณด๋Š” ๊ฒฝ์šฐ ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋Š” ์ฃผ๋กœ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์ด ์กฐ๊ฑด ๋งŒ์กฑ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ๋‘ ๊ฐœ์˜ ์˜์—ญ (๊ฐ€๋Šฅ, ๋ถˆ๊ฐ€๋Šฅ)์œผ๋กœ ๋‚˜๋‰˜..
[์ฝ”๋”ฉ] ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ ๊ตฌํ•˜๊ธฐ - ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ• (Euclidean Algorithm) ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ• ์œ ํด๋ฆฌ๋“œ ํ˜ธ์ œ๋ฒ•์ด๋ž€ ๋‹ค์Œ์˜ ์„ฑ์งˆ์„ ์ด์šฉํ•˜์—ฌ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ (Greatest Common Divisor, GCD) ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค: ๋‘ ์ˆ˜ A์™€ B๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž (A > B) ์ด ๋•Œ, A์™€ B๋ฅผ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ R์ด๋ผ๊ณ  ํ•  ๋•Œ, A์™€ B์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์†Œ๋Š” B์™€ R์˜ ์ตœ๋Œ€ ๊ณต์•ฝ์ˆ˜์™€ ๊ฐ™๋‹ค. ํ•œ ๋งˆ๋””๋กœ GCD(A,B) = GCD(B, R)์ด ์„ฑ๋ฆฝํ•œ๋‹ค๋Š” ๊ฑด๋ฐ, ์กฐ๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋ฉด ์ด ํŠน์ง•์œผ๋กœ๋ถ€ํ„ฐ ์žฌ๊ท€์ ์ธ ์„ฑ์งˆ์„ ๋„์ถœํ•  ์ˆ˜ ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, B๊ณผ R ์—ญ์‹œ B > R์ด ์„ฑ๋ฆฝํ•˜๋ฏ€๋กœ B๋ฅผ R๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ X๋ผ๊ณ ํ•˜๋ฉด ๊ฒฐ๊ตญ GCD(A,B) = GCD(B,R) = GCD(R,X) = ... ์ด ์„ฑ๋ฆฝํ•œ๋‹ค. ์ด ๋•Œ, ๋‘ ๊ฐ’์„ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๊ฐ€ 0์ผ ๋•Œ ๊ฐ’๋“ค์˜ ์•ฝ์ˆ˜๋Š” ํ•˜๋‚˜์ด๋ฏ€๋กœ ์ตœ์ข…์ ์œผ๋กœ ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜..
[๋ชจ์•„ ๋†“๊ณ  ๋งํ•ด๋ณด์ž] ์ •๋ ฌ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ •๋ ฌ์ด ํ•„์š”ํ•œ ์ด์œ  = ํƒ์ƒ‰ ๋ฐ์ดํ„ฐ๊ฐ€ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค๋ฉด '์ด์ง„ ํƒ์ƒ‰'์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด์ง„ ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์ตœ์•…์˜ ๊ฒฝ์šฐ์—”์„œ O(log n)์˜ ์„ฑ๋Šฅ์„ ๋ณด์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ•๋ ฅํ•˜๋ฉฐ, ์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ์ •๋ ฌ์ด ์„ ํ–‰๋˜์–ด์•ผํ•œ๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ [Bubble Sort] ํ‰๊ท  ์‹คํ–‰ ์‹œ๊ฐ„ : $O(n^2)$ / ์ตœ์•… ์‹คํ–‰ ์‹œ๊ฐ„ : $O(n^2)$ / ๋ฉ”๋ชจ๋ฆฌ $O(1)$ ํ•œ ๋ฒˆ iteration์„ ๋Œ๋•Œ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ๊ฐ’์„ ์ •๋ ฌํ•œ๋‹ค. ํ•œ ๋ฒˆ์˜ iteration์—์„œ ์ฒซ๋ฒˆ์งธ, ๋‘๋ฒˆ์งธ๋ฅผ ๋น„๊ต, ๋‘๋ฒˆ์งธ์™€ ์„ธ๋ฒˆ์งธ๋ฅผ ๋น„๊ต ... ์ด๋Ÿฐ ํ˜•ํƒœ๋กœ $n-1$๋ฒˆ์งธ์™€ $n$๋ฒˆ์งธ๋ฅผ ๋น„๊ตํ•จ์œผ๋กœ์จ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์ •๋ ฌํ•œ ํ›„ ๋‹ค์‹œ ์•ž์—์„œ๋ถ€ํ„ฐ ๋น„๊ตํ•ด์„œ $n-1$๋ฒˆ์งธ ๊ฐ’์„ ์ •๋ ฌํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋™์ž‘ํ•œ๋‹ค. ์ตœ์•…์˜ ๊ฒฝ์šฐ์— ๋งค iteration๋งˆ๋‹ค ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋‹ค ..
sphinx-js ๋กœ javascript ์ฝ”๋“œ ๋ฌธ์„œํ™”ํ•˜๊ธฐ ๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ๋‹จ/ํ•˜๋‹จ์— ๊ฐ„ํ—์ ์œผ๋กœ ๋œจ๋Š” ๊ด‘๊ณ ๋Š” ๋ธ”๋กœ๊ทธ ๊ฐœ์ธ ์ˆ˜์ต๊ณผ ๊ด€๋ จ ์—†๋Š” ํ‹ฐ์Šคํ† ๋ฆฌ ์ธก ๊ด‘๊ณ ์ž…๋‹ˆ๋‹ค ์†Œ๊ฐœ ์•ˆ๋…•ํ•˜์„ธ์š”. ์˜ค๋Š˜์€ javascript๋กœ ์ฝ”๋”ฉ๋œ ํ”„๋กœ์ ํŠธ์˜ ๋ฌธ์„œํ™”๋ฅผ ์œ„ํ•œ sphinx-js๋ฅผ ์„ค๋ช…ํ•ด๋“œ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. sphinx๋Š” python ํ”„๋กœ์ ํŠธ์˜ ๋ฌธ์„œ ์ž๋™ํ™”๋ฅผ ์œ„ํ•œ ํˆด์ž…๋‹ˆ๋‹ค. python ํ”„๋กœ์ ํŠธ์˜ ํ•จ์ˆ˜, ๋ชจ๋“ˆ๋“ค์— ํŠน์ • ๋ฌธ๋ฒ•์„ ๋”ฐ๋ผ ์ฃผ์„์„ ์ž‘์„ฑํ•ด ๋‘๋ฉด ์ž๋™์œผ๋กœ latex ๋ฌธ์„œ, html๋กœ ๋นŒ๋“œํ•ด ์ฃผ๋Š” ํˆด์ด์—์š”. ์˜คํ”ˆ ์†Œ์Šค ํ”„๋กœ์ ํŠธ์˜ ๋ฌธ์„œํ™” ๋“ฑ์— ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. javascript์šฉ์œผ๋กœ ์œ ์‚ฌํ•œ ํˆด jsdoc ์ด ์žˆ์ง€๋งŒ, read-the-docs ์™€ ์—ฐ๋™ํ•ด์„œ ๋นŒ๋“œํ•˜๊ณ  ์‹ถ์–ด์„œ ์ฐพ๊ฒŒ ๋œ ๊ฒŒ ๋ฐ”๋กœ sphinx-js์˜ˆ์š”. GitHub - mozilla/sphinx-js: Autodoc-style ex..
No module named 'tensorboard' ํ•ด๊ฒฐ [์ฆ์ƒ] ์ž˜ ์‚ฌ์šฉ์ค‘์ด๋˜ conda ํ™˜๊ฒฝ์—์„œ tensorboard๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ์—๋Ÿฌ๊ฐ€ ๋–ด๋‹ค. ๋‹ค๋ฅธ ๊ฐ€์ƒ ํ™˜๊ฒฝ๋“ค์—์„œ๋„ ๋ชจ๋‘ ๋™์ผํ•œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๊ฐ€ ๋ฐœ๊ฒฌ๋˜์—ˆ๋‹ค. pip ๋ž‘ conda๋กœ tensorboard ํŒจํ‚ค์ง€ ์‚ญ์ œ๋ฅผ ํ•ด์คฌ๋Š”๋ฐ๋„ ์—๋Ÿฌ๊ฐ€ ๊ณ„์† ๋–ด๋‹ค. Traceback (most recent call last): File "~/.local/bin/tensorboard", line 7, in line 7, in from tensorboard.main import run_main ImportError: No module named 'tensorboard' [ํ•ด๊ฒฐ] bin ํด๋”๋ฅผ ์•„์˜ˆ ์‚ญ์ œํ–ˆ๋‹ค rm -rf ~/.bin/tensorboard ๋‹ค์‹œ conda ํ™˜๊ฒฝ ์—์„œ ๋ชจ๋“ˆ์„ ์žฌ ์„ค์น˜ํ•ด์ฃผ์—ˆ๋‹ค. pip install..
[pytorch] RuntimeError: one_hot is only applicable to index tensor import torch.nn.functional as F bs = 10 F.one_hot(torch.ones(bs)*2, 5) 2์— ๋Œ€์‘๋˜๋Š” ์ „์ฒด ํฌ๊ธฐ๋Š” 5์ธ one hot encoding vector๋ฅผ batch size๊ฐœ์ˆ˜๋งŒํผ ๋งŒ๋“ค๊ณ  ์‹ถ์–ด์„œ ์œ„์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค. RuntimeError: one_hot is only applicable to index tensor. one_hot ํ•จ์ˆ˜๋Š” index tensor (ํƒ€์ž… ๋ช…์ด torch.int64)๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ์ด๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. >>> torch.nn.functional.one_hot((torch.ones(bs)*2).to(torch.int64), 5) tensor([[0, 0, 1, 0..
[python] Anaconda ํ™˜๊ฒฝ yamlํŒŒ์ผ export ๋ฐ import (ํ™˜๊ฒฝ ์ƒ์„ฑ create, ํ™œ์„ฑํ™” activate ๋“ฑ) Anaconda ๋Š” python ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ๋ฅผ ์ง€์›ํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. Anaconda๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์žฅ์  ์ค‘์— ํ•˜๋‚˜๋Š”, ์šฉ๋„์— ๋งž์ถ”์–ด ๊ฐ€์ƒ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ  ๊ทธ ํ™˜๊ฒฝ ๋‚ด์—์„œ๋งŒ ํŒจํ‚ค์ง€ ์˜์กด์„ฑ์„ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ์ด๊ฒŒ ์™œ ํฐ ์žฅ์ ์ด ๋˜๋Š๋ƒ, ์ด ๊ฐ€์ƒ ํ™˜๊ฒฝ (environment)๋งŒ ์˜ฎ๊ฒจ์ฃผ๋ฉด ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด์ฃ . ์ด๋Ÿฌํ•œ ๊ธฐ๋Šฅ์€ ์ฝ”๋“œ ๋ฐฐํฌ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค. ๊ณต๊ฐœ ์†Œํ”„ํŠธ์›จ์–ด๋‚˜, ๋‚ด ๋…ผ๋ฌธ์„ ๊ตฌํ˜„ ์ฝ”๋“œ๋ฅผ ๊ณต๊ฐœํ•˜๊ณ ์žํ•  ๋•Œ, ๊ฐœ๋ฐœํ•˜๊ณ ์žํ•˜๋Š” ์‘์šฉ์„์œ„ํ•œ ์ „์šฉ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ , ๊ทธ ํ™˜๊ฒฝ์„ ๊ทธ๋Œ€๋กœ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ๋„๋กํ•˜๋Š” ์˜์กด์„ฑ ์ •๋ณด๋งŒ ๋ฐฐํฌํ•ด์ฃผ๋ฉด ๋ˆ„๊ตฌ๋“ ์ง€ ๋™์ผํ•œ ํ™˜๊ฒฝ์„ ์žฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ค๋Š˜์€ ๊ฐ€์ƒ ํ™˜๊ฒฝ ๊ด€๋ฆฌ์™€ ๊ด€๋ จ๋œ ๋ช‡๊ฐ€์ง€๋ฅผ ๊ฐ„๋žตํžˆ ์ •๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋ชฉ์ฐจ 1. conda ํ™˜๊ฒฝ ..