~imadnyc/EE-env-python

f4979404bf0a9edd4882edcac7eafae200d481a3 — imad.nyc 3 months ago f2a6657
Updated final? script
4 files changed, 197 insertions(+), 27 deletions(-)

M .ipynb_checkpoints/hw6-checkpoint.ipynb
M hw6.ipynb
M poetry.lock
M pyproject.toml
M .ipynb_checkpoints/hw6-checkpoint.ipynb => .ipynb_checkpoints/hw6-checkpoint.ipynb +100 -6
@@ 2,7 2,7 @@
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "execution_count": 1,
   "id": "2af5827b",
   "metadata": {},
   "outputs": [


@@ 11,7 11,7 @@
     "output_type": "stream",
     "text": [
      "(-59.99999999999997+103.92304845413264j)\n",
      "Solutions: [-60.0 + 103.923048454133*I]\n"
      "Solutions: [20.0 + 20.0*I]\n"
     ]
    }
   ],


@@ 26,7 26,7 @@
    "\n",
    "\n",
    "z = symbols('z')\n",
    "equation = Eq(z /2, voltage2)\n",
    "equation = Eq(z /2, 10+10j)\n",
    "\n",
    "solutions = solve(equation, z)\n",
    " \n",


@@ 37,11 37,105 @@
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "execution_count": 56,
   "id": "68d95f7e-e869-4b32-a487-292d85e5c4ee",
   "metadata": {},
   "outputs": [],
   "source": []
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IPython console for SymPy 1.12 (Python 3.11.7-64-bit) (ground types: python)\n",
      "\n",
      "These commands were executed:\n",
      ">>> from sympy import *\n",
      ">>> x, y, z, t = symbols('x y z t')\n",
      ">>> k, m, n = symbols('k m n', integer=True)\n",
      ">>> f, g, h = symbols('f g h', cls=Function)\n",
      ">>> init_printing()\n",
      "\n",
      "Documentation can be found at https://docs.sympy.org/1.12/\n",
      "\n",
      "\u001b[1m equation1 \u001b[0m is a*(6.0 - 8.0*I) + (6.0 - 8.0*I)*(a - b) - 675.0 - 389.711431702\n",
      "997*I\n",
      "\u001b[1m equation2 \u001b[0m is b*(6.0 - 8.0*I) + (6.0 - 8.0*I)*(-a + b) + 779.422863405995*I\n",
      "\u001b[1m solutions \u001b[0m is [{a: 27.0 + 36.0*I, b: 44.6769145362398 - 5.38268590217986*I}]\n",
      "\u001b[1m This will print the symbols of the variable, not the variable. Look at your symbols above. \u001b[0m\n",
      "\u001b[1m a \u001b[0m rectangular form is (27.00000000000003+35.99999999999998j)\n",
      "\u001b[1m a \u001b[0m phasor form is 45.0 ∠ 53.13°\n",
      "\u001b[1m b \u001b[0m rectangular form is (44.67691453623981-5.38268590217986j)\n",
      "\u001b[1m b \u001b[0m phasor form is 45.0 ∠ -6.87°\n",
      "\u001b[1m i2-i1 \u001b[0m rectangular form is (17.676914536239785-41.38268590217984j)\n",
      "\u001b[1m i2-i1 \u001b[0m phasor form is 45.0 ∠ -66.87°\n",
      "\u001b[1m i2 * -1 \u001b[0m rectangular form is (-44.67691453623981+5.38268590217986j)\n",
      "\u001b[1m i2 * -1 \u001b[0m phasor form is 45.0 ∠ 173.13°\n"
     ]
    }
   ],
   "source": [
    "import electricpy as ep\n",
    "from sympy import symbols, Eq, solve, I\n",
    "from varname import varname\n",
    "from __future__ import print_function\n",
    "from io import StringIO\n",
    "import sys\n",
    "from sympy import init_session\n",
    "\n",
    "init_session() \n",
    "#TODO fix making printing and enetering more expressions more elegant\n",
    "#TODO add pretty printing from sympy\n",
    "\n",
    "def debug(expression):\n",
    "    frame = sys._getframe(1)\n",
    "    # weird codes are just bolding and unbolding\n",
    "    print('\\033[1m', expression, '\\033[0m', 'is ', end=\"\")\n",
    "    pprint(repr(eval(expression, frame.f_globals, frame.f_locals)))\n",
    "    \n",
    "def print_rect_and_phasor(name, value):\n",
    "    print('\\033[1m', name, '\\033[0m', \"rectangular form is\", value)\n",
    "    print('\\033[1m', name, '\\033[0m', \"phasor form is \", end =\"\")\n",
    "    ep.cprint(value)\n",
    "    \n",
    "# declare what things you want to solve for. If I have i1, i2, v1, and v2, but I only need i1 and i2, \n",
    "# I will only add those to varlist\n",
    "varlist = [i1,i2]\n",
    "\n",
    "i1 = symbols('a')\n",
    "i2 = symbols('b')\n",
    "\n",
    "# write your equations here \n",
    "equation1 = (ep.phasor(-450, 0) + ((6-8j) * (i1)) + ((6-8j) * (i1-i2)) + ep.phasor(450, -120))\n",
    "debug('equation1')\n",
    "\n",
    "equation2 = (ep.phasor(-450,-120) + (6-8j) * (i2-i1) + i2 * (6-8j) + (ep.phasor(450,120)))\n",
    "debug('equation2')\n",
    "\n",
    "#solve the equations\n",
    "solutions = solve([equation1, equation2], varlist, dict=True)\n",
    "debug('solutions')\n",
    "\n",
    "\n",
    "print('\\033[1m', \"This will print the symbols of the variable, not the variable. Look at your symbols above.\", '\\033[0m')\n",
    "solutions = solutions[0]\n",
    "for count, value in enumerate(varlist):\n",
    "    temp = solutions[value]\n",
    "    temp = complex(temp)\n",
    "    print_rect_and_phasor(value,temp)\n",
    "    \n",
    "\n",
    "#Get whatever else you need\n",
    "#TODO: make this more elegant\n",
    "print_rect_and_phasor('i2-i1', complex(solutions[i2]-solutions[i1]))\n",
    "print_rect_and_phasor('i2 * -1', complex(solutions[i2] * -1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f569a832-f0b8-422b-8075-7ba70211a085",
   "metadata": {},
   "source": [
    "y"
   ]
  }
 ],
 "metadata": {

M hw6.ipynb => hw6.ipynb +77 -20
@@ 2,46 2,103 @@
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2af5827b",
   "execution_count": 76,
   "id": "68d95f7e-e869-4b32-a487-292d85e5c4ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-59.99999999999997+103.92304845413264j)\n",
      "Solutions: [-60.0 + 103.923048454133*I]\n"
      "\n",
      "\u001b[1m equation1 \u001b[0m is i₁⋅(6.0 - 8.0⋅ⅉ) + (6.0 - 8.0⋅ⅉ)⋅(i₁ - i₂) - 675.0 - 389.711431\n",
      "702997⋅ⅉ\n",
      "\u001b[1m equation2 \u001b[0m is i₂⋅(6.0 - 8.0⋅ⅉ) + (6.0 - 8.0⋅ⅉ)⋅(-i₁ + i₂) + 779.422863405995⋅\n",
      "ⅉ\n",
      "\u001b[1m solutions \u001b[0m is [{i₁: 27.0 + 36.0⋅ⅉ, i₂: 44.6769145362398 - 5.38268590217986⋅ⅉ}\n",
      "]\n",
      "\u001b[1m This will print the symbols of the variable, not the variable. Look at your symbols above. \u001b[0m\n",
      "\u001b[1m i1 \u001b[0m rectangular form is (27.00000000000003+35.99999999999998j)\n",
      "\u001b[1m i1 \u001b[0m phasor form is 45.0 ∠ 53.13°\n",
      "\n",
      "\u001b[1m i2 \u001b[0m rectangular form is (44.67691453623981-5.38268590217986j)\n",
      "\u001b[1m i2 \u001b[0m phasor form is 45.0 ∠ -6.87°\n",
      "\n",
      "\u001b[1m i2-i1 \u001b[0m rectangular form is (17.676914536239785-41.38268590217984j)\n",
      "\u001b[1m i2-i1 \u001b[0m phasor form is 45.0 ∠ -66.87°\n",
      "\n",
      "\u001b[1m i2 * -1 \u001b[0m rectangular form is (-44.67691453623981+5.38268590217986j)\n",
      "\u001b[1m i2 * -1 \u001b[0m phasor form is 45.0 ∠ 173.13°\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import electricpy as ep\n",
    "from sympy import symbols, Eq, solve, I\n",
    "voltage = ep.phasor(60, -120)\n",
    "voltage2 = ep.phasor(60, 120)\n",
    "voltage3 = voltage + voltage2\n",
    "from varname import varname\n",
    "from __future__ import print_function\n",
    "from io import StringIO\n",
    "import sys\n",
    "from sympy import init_session\n",
    "\n",
    "init_session(quiet=True) \n",
    "#TODO fix making printing and enetering more expressions more elegant\n",
    "\n",
    "def equation_print(name, variable):\n",
    "    # weird codes are just bolding and unbolding\n",
    "    print('\\033[1m', name, '\\033[0m', 'is ', end=\"\")\n",
    "    pprint(variable, imaginary_unit = \"j\")\n",
    "    \n",
    "def print_rect_and_phasor(name, value):\n",
    "    value = complex(value)\n",
    "    print('\\033[1m', name, '\\033[0m', \"rectangular form is\", value)\n",
    "    print('\\033[1m', name, '\\033[0m', \"phasor form is \", end =\"\")\n",
    "    ep.cprint(value)\n",
    "    print()\n",
    "    \n",
    "#declare what things you want to solve for. If I have i1, i2, v1, and v2, but I only need i1 and i2, \n",
    "#I will only add those to varlist\n",
    "varlist = [i1,i2]\n",
    "\n",
    "i1 = symbols('i1')\n",
    "i2 = symbols('i2')\n",
    "\n",
    "#write your equations here \n",
    "equations = [equation1, equation2]\n",
    "equation1 = (ep.phasor(-450, 0) + ((6-8j) * (i1)) + ((6-8j) * (i1-i2)) + ep.phasor(450, -120))\n",
    "equation_print('equation1', equation1)\n",
    "\n",
    "print(voltage2 * 2)\n",
    "equation2 = (ep.phasor(-450,-120) + (6-8j) * (i2-i1) + i2 * (6-8j) + (ep.phasor(450,120)))\n",
    "equation_print('equation2', equation2)\n",
    "\n",
    "#solve the equations\n",
    "solutions = solve(equations, varlist, dict=True)\n",
    "equation_print('solutions', solutions)\n",
    "\n",
    "z = symbols('z')\n",
    "equation = Eq(z /2, voltage2)\n",
    "#print the solutions\n",
    "print('\\033[1m', \"This will print the symbols of the variable, not the variable. Look at your symbols above.\", '\\033[0m')\n",
    "solutions = solutions[0]\n",
    "for count, value in enumerate(varlist):\n",
    "    temp = solutions[value]\n",
    "    temp = complex(temp)\n",
    "    print_rect_and_phasor(value,temp)\n",
    "    \n",
    "\n",
    "solutions = solve(equation, z)\n",
    " \n",
    "# printing solutions\n",
    "print(\"Solutions:\", solutions)\n",
    "\n"
    "#Get whatever else you need\n",
    "#TODO: make this more elegant\n",
    "s = solutions\n",
    "print_rect_and_phasor('i2-i1', s[i2]-s[i1])\n",
    "print_rect_and_phasor('i2 * -1', s[i2] * -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68d95f7e-e869-4b32-a487-292d85e5c4ee",
   "cell_type": "markdown",
   "id": "f569a832-f0b8-422b-8075-7ba70211a085",
   "metadata": {},
   "outputs": [],
   "source": []
   "source": [
    "y"
   ]
  }
 ],
 "metadata": {

M poetry.lock => poetry.lock +18 -1
@@ 2543,6 2543,23 @@ socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"]
zstd = ["zstandard (>=0.18.0)"]

[[package]]
name = "varname"
version = "0.13.0"
description = "Dark magics about variable names in python."
optional = false
python-versions = ">=3.8,<4.0"
files = [
    {file = "varname-0.13.0-py3-none-any.whl", hash = "sha256:d41101d69d92d167e15f2997039c0a52a73ff0c8e02256c3c8b549a11fc4b176"},
    {file = "varname-0.13.0.tar.gz", hash = "sha256:64e9052029fd4d49686ac6443a9ed182c2c149686e42acef69ebaa3a27811beb"},
]

[package.dependencies]
executing = ">=2.0,<3.0"

[package.extras]
all = ["asttokens (==2.*)", "pure_eval (==0.*)"]

[[package]]
name = "wcwidth"
version = "0.2.13"
description = "Measures the displayed width of unicode strings in a terminal"


@@ 2598,4 2615,4 @@ test = ["websockets"]
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "ae0dac7bc10ec22f4bef7aabb23cff93f67b7a64c36a602c6570dce33865f86a"
content-hash = "2f252c702cde2c33ec473297c5eddb16be7b6730290b3b7430c42edf08b18e11"

M pyproject.toml => pyproject.toml +2 -0
@@ 10,6 10,8 @@ readme = "README.md"
python = "^3.11"
electricpy = "^0.3.0"
jupyterthemes = "^0.20.0"
varname = "^0.13.0"
matplotlib = "^3.8.2"


[build-system]