Multiply Two Matrices

0 likes • May 31, 2023 • 0 views
Python

More Python Posts

delay time lambda

0 likes • Nov 19, 2022 • 0 views
Python
```from time import sleep
def delay(fn, ms, *args):  sleep(ms / 1000)  return fn(*args)
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second```

0 likes • Nov 19, 2022 • 1 view
Python
```# Python program for implementation of Radix Sort	# A function to do counting sort of arr[] according to# the digit represented by exp.def countingSort(arr, exp1):		n = len(arr)		# The output array elements that will have sorted arr	output = [0] * (n)		# initialize count array as 0	count = [0] * (10)		# Store count of occurrences in count[]	for i in range(0, n):		index = (arr[i]/exp1)		count[int((index)%10)] += 1		# Change count[i] so that count[i] now contains actual	# position of this digit in output array	for i in range(1,10):		count[i] += count[i-1]		# Build the output array	i = n-1	while i>=0:		index = (arr[i]/exp1)		output[ count[ int((index)%10) ] - 1] = arr[i]		count[int((index)%10)] -= 1		i -= 1		# Copying the output array to arr[],	# so that arr now contains sorted numbers	i = 0	for i in range(0,len(arr)):		arr[i] = output[i]
# Find the maximum number to know number of digits	max1 = max(arr)
# Do counting sort for every digit. Note that instead	# of passing digit number, exp is passed. exp is 10^i	# where i is current digit number	exp = 1	while max1/exp > 0:		countingSort(arr,exp)		exp *= 10
# Driver code to test abovearr = [ 170, 45, 75, 90, 802, 24, 2, 66]radixSort(arr)
for i in range(len(arr)):	print(arr[i]),```

find parity outliers

0 likes • Nov 19, 2022 • 0 views
Python
```from collections import Counter
def find_parity_outliers(nums):  return [    x for x in nums    if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0]  ]
find_parity_outliers([1, 2, 3, 4, 6]) # [1, 3]```

UNT CSCE 2100 Assignment 6

0 likes • Nov 18, 2022 • 0 views
Python
```"""Assignment 6
The goal is to make a graph ofwho bit who and who was bitten.There should be 10 nodes and 15 edges.3 arrows of biting each other and3 arrows of someone biting themselves.Networkx can not do self bitingarrows, but it is in the code."""
from graphviz import Digraph as DDotGraphfrom graphviz import Graph as UDotGraphimport networkx as nxfrom networkx.algorithms.dag import transitive_closureimport graphviz as gvimport matplotlib.pyplot as pltimport numpy as npfrom numpy.linalg import matrix_power
"""class DGraph:    def __init__(self):        self.d = dict()
def clear(self):        self.d = dict()
def add_node(self,n):        if not self.d.get(n):            self.d[n] = set()
def edges(self):        for f in self.d:            for t in self.d[f]:                yield (f,t)
def number_of_nodes(self):        return len(self.d)
def __repr__(self):        return self.d.__repr__()
def show(self):        dot = gv.Digraph()        for e in self.edges():            #print(e)            f, t = e            dot.edge(str(f), str(t), label='')        #print(dot.source)        show(dot)
# displays graph with graphvizdef show(dot, show=True, file_name='graph.gv'):    dot.render(file_name, view=show)

def showGraph(g,label="",directed=True):    if directed:        dot = gv.Digraph()    else:        dot = gv.Graph()
for e in g.edges():        print(e)        f, t = e        dot.edge(str(f), str(t), label=label)    print(dot.source)    show(dot)

bit()
#bitten()
T = transitive_closure(D)
for e in D.edges(): print(e)for n in D.nodes(): print(n)
# M = nx.adjacency_matrix(D)# MT = nx.adjacency_matrix(T)M = nx.to_numpy_matrix(D)MT = nx.to_numpy_matrix(T)M2 = M@M
def mPower(M, k): #M is numpy matrix    assert k >= 1    P = M    for _ in range(k):       P = P @ M    return P
def tc(M):    #compute transitive closure    pass
D1 = nx.DiGraph(M)D2 = nx.DiGraph(M2)
print('Matrix for Original\n', M)N = nx.to_numpy_array(D,dtype=int)print('np_array for Original\n', N)print('\nMatrix for Transitive Closure\n', MT)N2 = nx.to_numpy_array(T,dtype=int)print('np_array for Transitive Closure\n', N2)
show(D) #can use D, T, and numpy matrix power operationshow(T)show(T)```

0 likes • Oct 7, 2022 • 0 views
Python
```import pandas as pd

```def sum_of_powers(end, power = 2, start = 1):  return sum([(i) ** power for i in range(start, end + 1)])