Skip to content

Instantly share code, notes, and snippets.

@omitakahiro
omitakahiro / trie.py
Created July 26, 2023 01:09
(Naive) Python Implementation of Trie
from __future__ import annotations
class TrieNode:
def __init__(self, char: str):
self.char = char
self.is_end = False
self.children = {}
def add_child(self, char: str) -> None:
@omitakahiro
omitakahiro / stopwatch.md
Last active February 17, 2020 01:23
[Python] decorator to measure execution time of a function
import time

def stopwatch(info):
    def _decorator(func):
        def wrapper(*args, **kwargs):
            t1 = time.time()
            output = func(*args, **kwargs)
            t2 = time.time()
 print('%s: %.3fms' % (info,(t2-t1)*1000) )
@omitakahiro
omitakahiro / SparseCholesky.md
Last active February 10, 2023 01:08
[Python, Scipy] Sparse Cholesky decomposition

Scipy does not currently provide a routine for cholesky decomposition of a sparse matrix, and one have to rely on another external package such as scikit.sparse for the purpose. Here I implement cholesky decomposition of a sparse matrix only using scipy functions. Our implementation relies on sparse LU deconposition.

The following function receives a sparse symmetric positive-definite matrix A and returns a spase lower triangular matrix L such that A = LL^T.

from scipy.sparse import linalg as splinalg
import scipy.sparse as sparse
import sys

def sparse_cholesky(A): # The input matrix A must be a sparse symmetric positive-definite.