Пример #1
0
def asAMatrix(spl):
    if isinstance(spl, obj.MSSample) or not spl.spectra:
        return
    matrix_=[]
    for spectrum in spl.spectra:
        matrix.append(spectrum.x_data)
    return matrix(matrix_)
Пример #2
0
def calcDCT():
    matrix = []
    for y in range(0, 8):
        row = []
        for x in range(0, 8):
            if(y == 0):
                row.append(1/sqrt(8))
            else:
                row.append(sqrt(2/8.0) * cos(((2*x+1)*y * pi)/16.0))
        matrix.append(row)

    return matrix
Пример #3
0
def qcolumn_vector(raw_numbers, quantities):
    '''
    A quick constructor for a column vector.
    
    >>> qcolumn_vector((1,2,3),(meter,second,mole))
         1  
     m  1.0 
     s  2.0 
    mol 3.0
    '''
    matrix = []
    for element in raw_numbers:
        matrix.append([element])
    return QuantMatrix(numpy.array(matrix), [list(quantities),[dimensionless()]])
Пример #4
0
def qcolumn_vector(raw_numbers, quantities):
    '''
    A quick constructor for a column vector.
    
    >>> qcolumn_vector((1,2,3),(meter,second,mole))
         1  
     m  1.0 
     s  2.0 
    mol 3.0
    '''
    matrix = []
    for element in raw_numbers:
        matrix.append([element])
    return QuantMatrix(numpy.array(matrix),
                       [list(quantities), [dimensionless()]])
Пример #5
0
def find_coeffs_similarity(pa, pb):

    matrix = []
    for p1, p2 in zip(pa, pb):
        matrix.append([p1[0], p1[1], 1, 0])
        matrix.append([p1[1], -p1[0], 0, 1])

    A = numpy.matrix(matrix, dtype=numpy.float)
    B = numpy.array(pb).reshape(2 * len(pa))

    res = numpy.dot(numpy.linalg.inv(A.T * A) * A.T, B)
    res = res[0][0]
    T = numpy.matrix([[res[0, 0], res[0, 1], res[0, 2]],
                      [-res[0, 1], res[0, 0], res[0, 3]]])

    return (T[0, 0], T[0, 1], T[0, 2], T[1, 0], T[1, 1], T[1, 2])
Пример #6
0
def _get_matrix(name, kmatrix):
    """Converts a numpy matrix into a matrix digestable by e.g. matlab.

	@param name Name of the matrix
	@param kmatrix The matrix
	@return String which contains a matrix digestable by e.g. matlab
	"""

    line = list()
    list_numbers = (int, long, float, double, ubyte, ushort, short, uint16,
                    int32, int64, uint32, uint64)
    matrix = []
    is_string = True

    try:
        # assume, all elements have same data type
        if isinstance(kmatrix[0, 0], list_numbers):
            is_string = False

        for x in range(kmatrix.shape[0]):
            for y in range(kmatrix.shape[1]):
                if is_string:
                    line.append("'%s'" % kmatrix[x, y])
                else:
                    line.append('%.9g' % kmatrix[x, y])
            matrix.append(', '.join(line))
            line = list()
    except IndexError:
        if isinstance(kmatrix[0], list_numbers):
            is_string = False

        for x in range(kmatrix.shape[0]):
            if is_string:
                line.append("'%s'" % kmatrix[x])
            else:
                line.append('%.9g' % kmatrix[x])
        matrix.append(', '.join(line))
        line = list()

    matrix = ';'.join(matrix)

    if is_string:
        matrix = ''.join([name, ' = {', matrix, '}'])
    else:
        matrix = ''.join([name, ' = [', matrix, ']'])

    return matrix.replace('\n', '')
Пример #7
0
def _get_matrix (name, kmatrix):
	"""Converts a numpy matrix into a matrix digestable by e.g. matlab.

	@param name Name of the matrix
	@param kmatrix The matrix
	@return String which contains a matrix digestable by e.g. matlab
	"""

	line=list()
	list_numbers=(int, long, float, double, ubyte, ushort, short, uint16, int32, int64, uint32, uint64)
	matrix=[]
	is_string=True

	try:
		# assume, all elements have same data type
		if isinstance(kmatrix[0, 0], list_numbers):
			is_string=False

		for x in range(kmatrix.shape[0]):
			for y in range(kmatrix.shape[1]):
				if is_string:
					line.append("'%s'" % kmatrix[x, y])
				else:
					line.append('%.9g' % kmatrix[x, y])
			matrix.append(', '.join(line))
			line=list()
	except IndexError:
		if isinstance(kmatrix[0], list_numbers):
			is_string=False

		for x in range(kmatrix.shape[0]):
			if is_string:
				line.append("'%s'" % kmatrix[x])
			else:
				line.append('%.9g' % kmatrix[x])
		matrix.append(', '.join(line))
		line=list()

	matrix=';'.join(matrix)

	if is_string:
		matrix=''.join([name, ' = {', matrix, '}'])
	else:
		matrix=''.join([name, ' = [', matrix, ']'])

	return matrix.replace('\n', '')
Пример #8
0
	def random_matrix(shape=[2,100], min=-1, max=1, type=REAL, include_threshold_multiplier=True):
		"""generates a 2-D list representing a matrix

    Retrieves rows pertaining to the given keys from the Table instance
    represented by big_table.  Silly things may happen if
    other_silly_variable is not None.

    Args:
        shape: The shape of the desired matrix
        min: The minimum value of any given value in the matrix 
        	(defaults to real numbers between 0-1 by default). 
        	This is exclusive i.e to get a min integer of 1 use min=0.
        max: The max value of any given value in the matrix. 
        
    Returns:
        A list representing a random generated matrix
        
    Raises:
    """
		matrix = []
		for i in range(shape[1]):
			matrix.append(DataGenerator.random_vector(shape[0], min, max, type, include_threshold_multiplier))
			
		return matrix	
Пример #9
0
    def build_matrix(self, constraints, point_to_matrix):
        n = len(self._all_points)
        targets = []
        matrix = []

        for (coeffs, target) in constraints:
            row = [0] * 2 * n
            for (pt, coeffx, coeffy) in coeffs:
                row[2 * point_to_matrix[pt]] = coeffx
                row[2 * point_to_matrix[pt] + 1] = coeffy
            matrix.append(row)
            targets.append(target)

        ortho_matrix = self.build_ortho(matrix)

        self._target_map_x = {}
        self._target_map_y = {}
        for pt in self._point_lru:
            coords = self._point_coords[pt]
            row1 = [0] * 2 * n
            row2 = [0] * 2 * n
            row1[2 * point_to_matrix[pt]] = 1
            row2[2 * point_to_matrix[pt] + 1] = 1

            if self.can_add(ortho_matrix, 2 * point_to_matrix[pt]):
                self._target_map_x[pt] = len(targets)
                targets.append(coords[0])
                matrix.append(row1)
                self.add_ortho_row(ortho_matrix, row1)

            if self.can_add(ortho_matrix, 2 * point_to_matrix[pt] + 1):
                self._target_map_y[pt] = len(targets)
                targets.append(coords[1])
                matrix.append(row2)
                self.add_ortho_row(ortho_matrix, row2)

            if len(matrix) == 2 * n:
                break

        # TODO: optimize this
        return (array(matrix), array(targets))
Пример #10
0
    def build_matrix(self, constraints, point_to_matrix):
        n = len(self._all_points)
        targets = []
        matrix = []

        for (coeffs, target) in constraints:
            row = [0] * 2 * n
            for (pt, coeffx, coeffy) in coeffs:
                row[2 * point_to_matrix[pt]] = coeffx
                row[2 * point_to_matrix[pt] + 1] = coeffy
            matrix.append(row)
            targets.append(target)

        ortho_matrix = self.build_ortho(matrix)

        self._target_map_x = {}
        self._target_map_y = {}
        for pt in self._point_lru:
            coords = self._point_coords[pt]
            row1 = [0] * 2 * n
            row2 = [0] * 2 * n
            row1[2 * point_to_matrix[pt]] = 1
            row2[2 * point_to_matrix[pt] + 1] = 1

            if self.can_add(ortho_matrix, 2 * point_to_matrix[pt]):
                self._target_map_x[pt] = len(targets)
                targets.append(coords[0])
                matrix.append(row1)
                self.add_ortho_row(ortho_matrix, row1)

            if self.can_add(ortho_matrix, 2 * point_to_matrix[pt] + 1):
                self._target_map_y[pt] = len(targets)
                targets.append(coords[1])
                matrix.append(row2)
                self.add_ortho_row(ortho_matrix, row2)

            if len(matrix) == 2 * n:
                break

        # TODO: optimize this
        return (array(matrix), array(targets))
 def get_responses_matrix(self):
     matrix = []
     for candidate in self.candidates:
         matrix.append(self.get_positions_from_candidate(candidate))
     return matrix
 def get_responses_matrix(self):
     matrix = []
     for candidate in self.candidates:
         matrix.append(self.get_positions_from_candidate(candidate))
     return matrix
Пример #13
0
def cubicSplineInterpolate(x_axis, y_axis, z_axis):
    '''
        prepare right-side vector
    '''
    dx = []
    dy = []
    dz = []
    matrix = []
    n = 2
    while n < len(x_axis):
        dx.append(3 * (x_axis[n] - 2 * x_axis[n - 1] + x_axis[n - 2]))
        dy.append(3 * (y_axis[n] - 2 * y_axis[n - 1] + y_axis[n - 2]))
        dz.append(3 * (z_axis[n] - 2 * z_axis[n - 1] + z_axis[n - 2]))
        n = n + 1
    '''
        produce square matrix looks like :
        [[2.0, 0.5, 0.0, 0.0], [0.5, 2.0, 0.5, 0.0], [0.0, 0.5, 2.0, 0.5], [0.0, 0.0, 2.0, 0.5]]
        the classes of the matrix depends on the length of x_axis(number of nodes)
    '''
    matrix.append([float(2), float(0.5)])
    for m in range(len(x_axis) - 4):
        matrix[0].append(float(0))
    n = 2
    while n < len(x_axis) - 2:
        matrix.append([])
        for m in range(n - 2):
            matrix[n - 1].append(float(0))

        matrix[n - 1].append(float(0.5))
        matrix[n - 1].append(float(2))
        matrix[n - 1].append(float(0.5))

        for m in range(len(x_axis) - n - 3):
            matrix[n - 1].append(float(0))
        n = n + 1

    matrix.append([])
    for m in range(n - 2):
        matrix[n - 1].append(float(0))
    matrix[n - 1].append(float(0.5))
    matrix[n - 1].append(float(2))
    '''
        LU Factorization may not be optimal method to solve this regular matrix.
        If you guys have better idea to solve the Equation, please contact me.
    '''
    P, L, U = doLUFactorization(matrix)
    u = solveEquations(P, L, U, dx)
    v = solveEquations(P, L, U, dy)
    w = solveEquations(P, L, U, dz)
    '''
        define gradient of start/end point
    '''
    m = 0
    U = [0]
    V = [0]
    W = [0]
    while m < len(u):
        U.append(u[m])
        V.append(v[m])
        W.append(w[m])
        m = m + 1
    U.append(0)
    V.append(0)
    W.append(0)

    plotCubicSpline(U, V, W, x_axis, y_axis, z_axis)
Пример #14
0
    elif kak == 2:
        jaj = randint(M, N)
        jazz = jazz + jaj
        ark.append(jaj)
    else:
        print("game over, snake\n")
        exit()
print(f"вот-с ваш массив - {ark}\n")
jazz = []
kazz = []
for i in range(len(ark)):
    if i % 2 == 0 and ark[i] > 0:
        jazz.append(ark[i])
        kazz.append(i)
jazz.sort()
matrix.append(kazz)
matrix.append(jazz)
print(matrix)
for j in range(len(matrix[0])):
    k = matrix[0][j]
    ark[k] = matrix[1][j]
print(ark)

print("tenth task\n")
ark = []
skok = int(input("сколько будет массивов ?\n"))
kak = int(input("хотите сами ввести числа ? -1 или автоматом ? -2 \n"))
if kak == 2:
    M = int(input("в таком случае введите от какого числа\n"))
    N = int(input("И до какого\n"))
jazz = 0