Skip to content

Instantly share code, notes, and snippets.

@MrSnor
Last active April 11, 2024 01:04
Show Gist options
  • Select an option

  • Save MrSnor/f00eda4b6653db7798f99d6a096a1f36 to your computer and use it in GitHub Desktop.

Select an option

Save MrSnor/f00eda4b6653db7798f99d6a096a1f36 to your computer and use it in GitHub Desktop.
IDSUP programs (chapterwise)
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Chapter 4 - Linear Algebra"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is no native vector data type in Python \n",
"\n",
"Representation of a vector in python - list \n",
"vec = [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example of using add function\n",
"[5, 7, 9]\n",
"Example of using subtract function\n",
"[-3, -3, -3]\n",
"Example of using dot function\n",
"32\n"
]
}
],
"source": [
"# to add 2 vectors v and w\n",
"from typing import List\n",
"# custom data type for a vector\n",
"vector = List[float]\n",
"\n",
"# addition of 2 vectors\n",
"def add(v: vector, w: vector) -> vector:\n",
" assert len(v) == len(w), \"vectors must be the same length\"\n",
" return [v_i + w_i for v_i, w_i in zip(v, w)]\n",
"\n",
"# example of using the function\n",
"print(\"Example of using add function\")\n",
"a = [1, 2, 3]\n",
"b = [4, 5, 6]\n",
"print(add(a, b))\n",
"\n",
"# subtraction of 2 vectors\n",
"def subtract(v: vector, w: vector) -> vector:\n",
" assert len(v) == len(w), \"vectors must be the same length\"\n",
" return [v_i - w_i for v_i, w_i in zip(v, w)]\n",
"\n",
"# example of using the function\n",
"print(\"Example of using subtract function\")\n",
"a = [1, 2, 3]\n",
"b = [4, 5, 6]\n",
"print(subtract(a, b))\n",
"\n",
"# dot product of 2 vectors\n",
"# let v = [v1, v2, v3] and w = [w1, w2, w3]\n",
"# v dot w = v1w1 + v2w2 + v3w3\n",
"def dot_product(v: vector, w: vector) -> float:\n",
" assert len(v) == len(w), \"vectors must be the same length\"\n",
" product = [v_i * w_i for v_i, w_i in zip(v, w)]\n",
" return sum(product)\n",
"\n",
"# example of using the function\n",
"print(\"Example of using dot function\")\n",
"a = [1, 2, 3]\n",
"b = [4, 5, 6]\n",
"print(dot_product(a, b))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Length of a vector in python \n",
"let v = [v1, v2, v3] \n",
"|v| = sqrt(v1^2 + v2^2 + v3^2) \n",
"\n",
"Length in vector means its magnitude"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example of using len_of_vector function\n",
"3.7416573867739413\n"
]
}
],
"source": [
"from typing import List\n",
"import math\n",
"\n",
"def len_of_vector(v: List[float]) -> float:\n",
" return math.sqrt(sum(v_i * v_i for v_i in v))\n",
"\n",
"# Example of using the function\n",
"print(\"Example of using len_of_vector function\")\n",
"a = [1, 2, 3]\n",
"print(len_of_vector(a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Q. Get the distance between any 2 vectors."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# let v = [x1, y1] and w = [x2, y2]\n",
"# distance = sqrt((x2-x1)^2 + (y2-y1)^2)\n",
"# or simply, distance = len_of_vector(subtract(v, w))\n",
"\n",
"from typing import List\n",
"\n",
"vector = List[float]\n",
"\n",
"def distance(v: vector, w: vector) -> float:\n",
" assert len(v) == len(w) and len(v) > 0 and len(w) > 0, \"vectors must be the same length\"\n",
" return len_of_vector(subtract(v, w))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matrix in python\n",
"\n",
"3x3 matrix in python \n",
"\n",
"matrix = [ \n",
" [1, 2, 3], \n",
" [4, 5, 6], \n",
" [7, 8, 9] \n",
" ] "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Find Average Of Vectors In Python\n",
"\n",
"Let V1 = [1, 2, 3], V2 = [4, 5, 6], V3 = [7, 8, 9]\n",
"\n",
"Average Of Vectors = (V1 + V2 + V3) / 3"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example of using avg_of_vectors function\n",
"[4.0, 5.0, 6.0]\n"
]
}
],
"source": [
"from typing import List\n",
"\n",
"vector = List[float]\n",
"\n",
"def avg_of_vectors(vectors: List[vector]) -> vector:\n",
" \n",
" # check if all vectors have the same length\n",
" len_all = len(vectors[0])\n",
" assert all(len(v) == len_all for v in vectors), \"vectors must be the same length\"\n",
" \n",
" return [(sum(v[i] for v in vectors) / len(vectors)) for i in range(len_all)]\n",
"\n",
"print(\"Example of using avg_of_vectors function\")\n",
"a = [1, 2, 3]\n",
"b = [4, 5, 6]\n",
"c = [7, 8, 9]\n",
"print(avg_of_vectors([a, b, c]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Shape Of A Matrix In Python\n",
"\n",
"Returns the shape of a matrix i.e number of rows and columns"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example of using shape_of_matrix function\n",
"(3, 3)\n"
]
}
],
"source": [
"# Shape Of A Matrix In Python\n",
"\n",
"def shape_of_matrix(matrix: List[List[float]]) -> tuple:\n",
" return (len(matrix), len(matrix[0]))\n",
"\n",
"print(\"Example of using shape_of_matrix function\")\n",
"a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
"print(shape_of_matrix(a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Q. Create a matrix with 3 rows and 4 columns."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example of using create_matrix function\n",
"[['00', '01', '02', '03'], ['10', '11', '12', '13'], ['20', '21', '22', '23']]\n"
]
}
],
"source": [
"# Create a matrix with 3 rows and 4 columns.\n",
"\n",
"from typing import List\n",
"matrix_type = List[List[float]]\n",
"\n",
"def create_matrix(rows: int, cols: int) -> matrix_type:\n",
" matrix = []\n",
" for i in range(rows):\n",
" row = []\n",
" for j in range(cols):\n",
" s_i = str(i)\n",
" s_j = str(j)\n",
" row.append(s_i + s_j)\n",
" matrix.append(row)\n",
" return matrix\n",
"\n",
"print(\"Example of using create_matrix function\")\n",
"a = create_matrix(3, 4)\n",
"print(a)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Q. Create identity matrix of n x n."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Example of using identity_matrix function\n",
"[[1, 0], [0, 1]]\n",
"[[1, 0, 0], [0, 1, 0], [0, 0, 1]]\n",
"[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]\n"
]
}
],
"source": [
"# Q. Create identity matrix of n x n.\n",
"\n",
"def identity_matrix(n: int):\n",
" matrix = []\n",
" for i in range(n):\n",
" row = []\n",
" for j in range(n):\n",
" if i == j:\n",
" row.append(1)\n",
" else:\n",
" row.append(0)\n",
" matrix.append(row)\n",
" return matrix\n",
"\n",
"print(\"Example of using identity_matrix function\")\n",
"# 2 x 2\n",
"a = identity_matrix(2)\n",
"print(a)\n",
"\n",
"# 3 x 3\n",
"a = identity_matrix(3)\n",
"print(a)\n",
"\n",
"# 4 x 4\n",
"b = identity_matrix(4)\n",
"print(b)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment