Skip to content

Instantly share code, notes, and snippets.

import numpy as np
import matplotlib.pyplot as plt
h = 1
p = np.pi*1/3
m = np.arange(1,101)
A_m = 2*h*np.sin(m*p)/(m**2*p*(np.pi-p))
x = np.linspace(0,np.pi,150)
# f = A_m* np.sin(m[np.newaxis,:]*x[:,np.newaxis])
f = A_m * np.sin(np.outer(x,m))
@silgon
silgon / jsonoutputparser_pydantic.py
Last active January 14, 2025 12:57
Example with JSONOutputParser, PydanticOutputParser, Pydantic and Optional Values
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.prompts import PromptTemplate
from pydantic import BaseModel, ValidationError
from typing import List, Optional
# Initialize the language model. Replace 'openai' with your specified model.
llm = ChatOpenAI(model="gpt-4o-mini",temperature=0)
import numpy as np
import matplotlib.pyplot as plt
p = np.linspace(0,1j*2*np.pi,50)
θ = np.linspace(0,np.pi/2,50)
P,Θ = np.meshgrid(p,θ)
m = np.exp(P)*np.sin(Θ)*np.cos(Θ) + np.sin(Θ)**2
b = m.flatten()
r,i = np.real(b), np.imag(b)
import matplotlib.pyplot as plt
from qiskit import *
from qiskit.tools.monitor import job_monitor
from qiskit.tools.visualization import plot_bloch_multivector, plot_histogram
from math import pi
qr = QuantumRegister(3, name="q")
crz, crx = ClassicalRegister(1, name="crz"), ClassicalRegister(1, name="crx")
c0 = ClassicalRegister(1, name="c0")
import qutip
import matplotlib.pyplot as plt
import numpy as np
b = qutip.Bloch()
# vec = [0,1,0]
th = np.linspace(0, np.pi, 20)
xz = np.sin(th)
yz = np.zeros(20)
zz = np.cos(th)
@silgon
silgon / exploit_structure.jl
Last active July 6, 2023 22:44
exploit_structure.jsl
using SparseArrays
using LinearAlgebra
using BandedMatrices
using SpecialMatrices
using ToeplitzMatrices
using Random
using BlockArrays
using FFTW
BLAS.set_num_threads(1)
import numpy as np
import cvxpy as cp
α = 0.3
A = np.array([[1., α], [α, 1.5]])
# maximum eigenvalue
t = cp.Variable()
prob = cp.Problem(cp.Minimize(t), [np.eye(2)*t-A>>0])
prob.solve()
# minimum eigenvalue
t = cp.Variable()

Sanity check for convex optimization set

image

import plotly.express as px
df = px.data.tips()
color_map = dict(Sun="#881122", Sat="#991188", Fri="#221122", Thur="#887722")
fig = px.sunburst(df, path=['day', 'time', 'sex'], color="day", values='total_bill',
color_discrete_map=color_map)
fig.show()
## No Express (without order)
def build_hierarchical_dataframe(df, levels, value_column, color_column, color_map):
levels_rev = levels[::-1]