Example #1
0
def matrix_random_matrixID(scalar_type, row_level, col_level, range_start, range_stop, sparsity=None):
    """
    Generate a matrix of size m x n, where m = 2^row_level and n = 2^col_level, with entries 
    in [range_start, range_stop). If a sparsity value in [0,1] is given, rounds 
    sparsity * m * n to the closest integer value and generates the matrix such that there 
    are exactly that many (randomly placed) zeros in the matrix. 
    """
    #size of matrix
    n = (2**row_level)*(2**col_level)

    # COMPLEX CASE
    if scalar_type.lower() in ["c", "complex"]:
        rand_f = rand_complex

    # INTEGER CASE
    elif scalar_type.lower() in ["i", "integer"]:
        rand_f = random.randrange
        #check that start and stop are integers
        if type(range_start) != int or type(range_stop) != int:
            raise ValueError("range_start and range_stop must be integers when scalarType = INTEGER!")

    # REAL CASE
    elif scalar_type.lower() in ["r", "real"]:
        rand_f = rand_real

    else:
        raise Exception("Do not know how to build matrix for type %s." %scalar_type)

    #apply sparsity requirements if present
    if sparsity is not None:
        #calculate required number of zeros from sparsity
        numZerosReq = int(round(sparsity * n))
        #randomly choose zero locations
        zeroPos = random.sample(range(n), numZerosReq)
        #generate random values in desired range, placing zeros
        randVals = []
        for i in range(n):
            if i in zeroPos:
                randVals.append(0)
            else:
                val = 0
                while val == 0:
                    val = rand_f(range_start, range_stop)
                randVals.append(val)
    #otherwise, just generate random values in desired range
    else: 
        randVals = [rand_f(range_start, range_stop) for i in range(n)]
    #convert list to a matrix and add to store
    try:
        randMat_ID = row_major_list_to_store_matrixID(pylarc.map_to_str(randVals, scalarTypeStr), row_level, col_level, 2**col_level)
    except TypeError:
        raise TypeError("argument 1 of type 'ScalarType'")

    #return matrix id
    return randMat_ID
Example #2
0
            for i in range(dim_whole)
        ]
    elif scalarTypeStr in ("Real", "MPReal", "MPRational"):
        randVals = [random.random() for i in range(dim_whole)]
    else:
        raise Exception('Do not know how to build matrix for type %s.' %
                        scalarTypeStr)

    # create circulant matrix from the dim_whole random numbers
    a = []
    b = randVals
    for i in range(dim_whole):
        a.append(list(b))
        b.insert(0, b.pop(-1))
    #print("a: ", a)
    amat = np.matrix(a)
    alist = amat.reshape(-1).tolist()[0]
    #print(alist)
    arr = pylarc.map_to_str(alist, scalarTypeStr)
    #print('arr:', pylarc.str_scalarTypeArray(arr, len(alist)))

    # creating or finding the matrix associated with the array
    serial = pylarc.row_major_list_to_store_matrixID(arr, level, level,
                                                     dim_whole)
    filename_json = "../dat/out/circulant.lev%d.%s.json" % (level,
                                                            scalarTypeStr)
    # pylarc.print_naive_by_matID(serial)
    # print("\n")
    pylarc.write_larcMatrix_file_by_matID(
        serial, os.path.join(os.path.dirname(__file__), filename_json))
Example #3
0
print("my first matrix is")
pylarc.print_naive_by_matID(myFirstMatID)

print("\nentry squared and then multiplied by 10 is")
squareID = pylarc.matrix_entrySquared_matrixID(myFirstMatID, "10")
pylarc.print_naive_by_matID(squareID)

myfile = "../tests/dat/out/temp_" + typeChar + "_matrix.json"
print(myfile)

pylarc.write_larcMatrix_file_by_matID(squareID, myfile)

# lets look at the result of building an identity and zero matrix
# from scratch using the row major reader and print
vals = [0 for i in range(64)]
vals_string = pylarc.map_to_str(vals, "Integer")
zeroID_3_3 = pylarc.row_major_list_to_store_matrixID(vals_string, 3, 3, 8)
print("The 8 by 8 Zero matrix is")
pylarc.print_naive_by_matID(zeroID_3_3)

vals = [1 * (0 == (i % 9)) for i in range(64)]
vals_string = pylarc.map_to_str(vals, "Integer")
identityID_3 = pylarc.row_major_list_to_store_matrixID(vals_string, 3, 3, 8)
print("The 8 by 8 identity matrix is")
pylarc.print_naive_by_matID(identityID_3)

# Usually one would grab the matrixID's for the zero and
# identity matrices from the preloaded matrix store, using
# the internal faster C code as follows.
normalway_zeroID_3_3 = pylarc.get_zero_matrixID(3, 3)
normalway_identityID_3 = pylarc.get_identity_matrixID(3)
Example #4
0
    elif scalarTypeStr in ("Complex", "MPComplex", "MPRatComplex"):
        a = np.matrix([[1 + 2j, 3 + 4j, 5 + 6j, 7 + 8j],
                       [8 + 7j, 6 + 5j, 3 + 4j, 1 + 2j],
                       [9 + 10j, 11 + 12j, 13 + 14j, 15 + 16j],
                       [16 + 15j, 14 + 13j, 12 + 11j, 10 + 9j]])
    elif scalarTypeStr in ("Real", "MPReal", "MPRational"):
        a = np.matrix([[1, 3, .5, 6], [8, 6, 3, .1], [-9, 11, 13, 1.5],
                       [16, 13, 12, 10]])
    else:
        raise Exception('Do not know how to build matrix for type %s.' %
                        scalarTypeStr)

    # test our new function matrix_is_zero_matrixID(z_matID))
    z = np.matrix([[0, 0], [0, 0]])
    zlist = z.reshape(-1).tolist()[0]
    zlist_strs = pylarc.map_to_str(zlist, scalarTypeStr)
    z_matID = pylarc.row_major_list_to_store_matrixID(zlist_strs, 1, 1, 2)
    pylarc.print_naive_by_matID(z_matID)
    print("\nThe fucntion matrix_is_zero_matrixID returns:")
    print(pylarc.matrix_is_zero_matrixID(z_matID))
    print("\n")

    # turn the matrix into an array by reading off each row in turn (row major format)
    alist = a.reshape(-1).tolist()[0]
    alist_strs = pylarc.map_to_str(alist, scalarTypeStr)
    # arr = pylarc.buildArray(alist)
    # print 'arr:', pylarc.str_scalarTypeArray(arr, len(alist))
    print("alist_str:", alist_strs)

    # parameters for entering the python array into the store
    level = 2