0

So I wrote a new way to create a matrix without the use of numpy or np zeros.

import numpy as np
import time
x = [1,2,3,4,5,6,7,8,9,10]
x1 = 1

def fakenpzeros(size):
    rows, cols = 1, size
    matrix = [([0]*cols) for i in range(rows)]
    return matrix

print(np.zeros(x1))
print(fakenpzeros(x1))

The console would output

[[0]] (for the function I created) and [0.] (for the numpy function)

I want to introduce my function into my code as I am trying to create a random sampling function without numpy or random

This is my following code:

def pseudo_uniform_bad(mult=5,
                       mod=11,
                       seed=1,
                       size=1):
    """
    A bad pseudo random generator with small multipliers and modulus
    """
    U = np.zeros(size)
    #U = fakenpzeros(size)
    x = (seed*mult+1)%mod
    U[0] = x/mod
    for i in range(1,size):
        x = (x*mult+1)%mod
        U[i] = x/mod
    return U

def pseudo_uniform_good(mult=16807,
                        mod=(2**31)-1,
                        seed=123456789,
                        size=1):
    """
    A reasoanbly good pseudo random generator
    """
    U = np.zeros(size)
    #U = fakenpzeros(size)
    x = (seed*mult+1)%mod
    U[0] = x/mod
    for i in range(1,size):
        x = (x*mult+1)%mod
        U[i] = x/mod
    return U

def pseudo_uniform(low=0,
                   high=1,
                  seed=123456789,
                  size=1):
    """
    Generates uniformly random number between `low` and `high` limits
    """
    return low+(high-low)*pseudo_uniform_good(seed=seed,size=size)

def sample_pick(lst):
    """
    Picks up a random sample from a given list
    """
    # Sets seed based on the decimal portion of the current system clock
    t = time.perf_counter()
    seed = int(10**9*float(str(t-int(t))[0:]))
    # Random sample as an index
    l = len(lst)
    s = pseudo_uniform(low=0,high=l,seed=seed,size=1)
    idx = int(s)
    
    return (lst[idx]) #SAMPLING
print(sample_pick(x1))

However, when I replace np.zeros(size) with fakenpzeros(size) it says something along the lines of:

unsupported operand type(s) for +: 'int' and 'list'

So how exactly can I convert my matrix into an integer/tuple of integers like how numpy does it?

ThatDaven
  • 27
  • 1
  • 8
  • Numpy does not convert a matrix to an integer/integer tuple, it just accepts an integer as the right operand of matrix addition and applies the addition to each element. – Mechanic Pig Sep 18 '22 at 12:25
  • So how exactly would I do that with my function also? @MechanicPig – ThatDaven Sep 18 '22 at 12:27
  • [Overloading Addition, Subtraction, and Multiplication Operators](https://stackoverflow.com/questions/20507745/overloading-addition-subtraction-and-multiplication-operators) and [Operator Overloading in Python](https://www.geeksforgeeks.org/operator-overloading-in-python/) – Mechanic Pig Sep 18 '22 at 12:29
  • @MechanicPig I don't exactly understand what the post above is saying, could you walk me through it? – ThatDaven Sep 18 '22 at 12:30
  • In the console, test `[1,2,3]+[4]`. You can 'add' a list to a list. You can't 'add' a list and an int. A list isn't a dropin replacement for an array. – hpaulj Sep 18 '22 at 14:48
  • Show the FULL error message, with traceback. In other words, identify exactly where you get the error. Then, examine the variables in that line, especially the ones involved with the '+'. Also, are you aware that your fakezeros is more like a (1,n) shaped np.zeros? `U[0]` will be quite different. You need to do more line by line testing. – hpaulj Sep 18 '22 at 15:43

0 Answers0