{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Scalar diff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sympy import (\n",
" symbols,\n",
" diff,\n",
" tan,\n",
" asinh,\n",
" cos,\n",
" sqrt,\n",
" hessian,\n",
" atanh,\n",
" asin,\n",
" acos,\n",
" atan2,\n",
" sinh,\n",
" cosh,\n",
" tanh,\n",
" asinh,\n",
" acosh,\n",
" atanh,\n",
" exp,\n",
" log,\n",
")\n",
"\n",
"# Define the variable\n",
"s = symbols(\"s\", real=True)\n",
"\n",
"# Define the function\n",
"##################### modify this #####################\n",
"f = 1 / acos(s) - exp(s)\n",
"#######################################################\n",
"\n",
"# Compute the derivative with respect to s\n",
"df = diff(f, s)\n",
"ddf = diff(diff(f, s))\n",
"\n",
"# symars: https://github.com/Da1sypetals/Symars\n",
"from symars import DType, GenScalar\n",
"from symars.scalar_cached import GenScalarCached\n",
"\n",
"# gen = GenScalar(DType.F64)\n",
"name = \"003\"\n",
"gen = GenScalarCached(DType.F64)\n",
"gcode = gen.generate(f\"grad_{name}\", df)\n",
"print(gcode)\n",
"\n",
"hcode = gen.generate(f\"hess_{name}\", expr=ddf)\n",
"print(hcode)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matrix diff"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sympy import symbols, diff, tan, asinh, cos, sqrt, hessian, atanh\n",
"\n",
"# Define the variable\n",
"s = symbols(\"s\", real=True)\n",
"\n",
"# Define the function\n",
"# f = s * tan(s) * asinh(s) - s**1.3 * cos(s) + sqrt(s)\n",
"f = (\n",
" s\n",
" * tan(s)\n",
" * asinh(s)\n",
" * atanh(s)\n",
" * s # .tan().mul(&s.asinh().mul(&s.atanh())).mul(&s)\n",
" - s**1.3 * cos(s) # .sub(&s.powf(1.3).mul(&s.cos()))\n",
" + sqrt(s) # .add(&s.sqrt())\n",
" - (s / 1.441 + 1 / s) # .sub(&s.div_value(1.441).add(&s.recip()))\n",
" - (-6.235 / s) # .sub(&(-125.235).div_var(&s))\n",
")\n",
"\n",
"# Compute the derivative with respect to s\n",
"df = diff(f, s)\n",
"ddf = diff(diff(f, s))\n",
"\n",
"# symars: https://github.com/Da1sypetals/Symars\n",
"from symars import DType, GenScalar\n",
"from symars.scalar_cached import GenScalarCached\n",
"\n",
"# gen = GenScalar(DType.F64)\n",
"name = \"alpha\"\n",
"gen = GenScalarCached(DType.F64)\n",
"gcode = gen.generate(f\"grad_{name}\", df)\n",
"# print(gcode)\n",
"\n",
"hcode = gen.generate(f\"hess_{name}\", expr=ddf)\n",
"print(hcode)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sympy import (\n",
" symbols,\n",
" diff,\n",
" tan,\n",
" asinh,\n",
" cos,\n",
" sqrt,\n",
" hessian,\n",
" atanh,\n",
" acosh,\n",
" sinh,\n",
" Abs,\n",
" cosh,\n",
" acos,\n",
" tanh,\n",
")\n",
"import sympy as sp\n",
"\n",
"# Define the variable\n",
"s = symbols(\"s\", positive=True)\n",
"\n",
"f = (\n",
" s * tan(s) * asinh(s) ** 2 * s # .tan().mul(&s.asinh().square()).mul(&s)\n",
" - s**1.3 * cos(s) # .sub(&s.powf(1.3).mul(&s.cos()))\n",
" + sqrt(s) # .add(&s.sqrt())\n",
" - (s / 1.441 + 1 / s) # .sub(&s.div_value(1.441).add(&s.recip()))\n",
" - (-6.235 / s + sinh(s**3)) # .sub(&(-6.235).div_var(&s).add(&s.powi(3).sinh()))\n",
" + (\n",
" s ** (-1.24) / abs(s / 12.4)\n",
" ) # .add(&(s.powf(-1.24).div(&s.div_value(12.4).abs())))\n",
")\n",
"\n",
"f = Abs(f)\n",
"\n",
"# Compute the derivative with respect to s\n",
"df = diff(f, s)\n",
"ddf = diff(diff(f, s))\n",
"\n",
"# Display the result\n",
"from symars import DType, GenScalar\n",
"from symars.scalar_cached import GenScalarCached\n",
"\n",
"# gen = GenScalar(DType.F64)\n",
"gen = GenScalarCached(DType.F64)\n",
"gcode = gen.generate(\"grad_3\", df)\n",
"print(gcode)\n",
"\n",
"# hcode = gen.generate(\"hess_3\", expr=ddf)\n",
"# print(hcode)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"s = symbols(\"s\", positive=True)\n",
"\n",
"f = cosh(s) * sinh(s) * (1.245 / s ** (-2)) + tanh(s)\n",
"\n",
"# Compute the derivative with respect to s\n",
"df = diff(f, s)\n",
"ddf = diff(diff(f, s))\n",
"\n",
"# Display the result\n",
"from symars import DType, GenScalar\n",
"from symars.scalar_cached import GenScalarCached\n",
"\n",
"# gen = GenScalar(DType.F64)\n",
"gen = GenScalarCached(DType.F64)\n",
"gcode = gen.generate(\"grad_0\", df)\n",
"print(gcode)\n",
"\n",
"hcode = gen.generate(\"hess_0\", expr=ddf)\n",
"print(hcode)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(6):\n",
" for j in range(6):\n",
" print(f\"let a{i*6+j} = self.get_unchecked(({i},{j})).clone();\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "playground",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}