linear-algebra

Create a vector length n with n entries 'x'

Create a vector length n with n entries 'x' Question: Example: n = 5 x = 3.5 Output: array([3.5, 3.5, 3.5, 3.5, 3.5]) My code: import numpy as np def init_all_x(n, x): np.all = [x]*n return np.all init_all_x(5, 3.5) My question: Why init_all_x(5, 3.5).shape cannot run? If my code is wrong, what is the correct …

Total answers: 4

tensorflow.linalg.eig throwing error UnboundLocalError: local variable 'out_dtype' referenced before assignment

tensorflow.linalg.eig throwing error UnboundLocalError: local variable 'out_dtype' referenced before assignment Question: I have below code import tensorflow as tf X_tf = tf.Variable([[25, 2, 9], [5, 26, -5], [3, 7, -1]]) lambdas_X_tf, V_X_tf = tf.linalg.eig(X_tf) when I execute it I get below error File "C:Usersu1.condaenvspy39libsite-packagestensorflowpythonutiltraceback_utils.py", line 153, in error_handler raise e.with_traceback(filtered_tb) from None File "C:Usersu1.condaenvspy39libsite-packagestensorflowpythonopslinalg_ops.py", line …

Total answers: 1

Solving a tridiagonal matrix in python

Solving a tridiagonal matrix in python Question: I have been looking at numerical methods to solve differential equations for chemical reactions. Usually I put the differential equation into a tridiagonal matrix using finite difference method, and then using a column vector for the boundary conditions. Once I have the matrix and vector I use scipy’s …

Total answers: 1

Solving Sylvester equations in PyTorch

Solving Sylvester equations in PyTorch Question: I’m trying to solve a Sylvester matrix equation of the form AX + XB = C From what I’ve seen, these equations are usually solved with the Bartels-Stewart algorithm taking successive Schur decompositions. I’m aware scipy.linalg already has a solve_sylvester function, but I’m integrating the solution to the Sylvester …

Total answers: 1

numpy computed eigenvalues incorrect?

numpy computed eigenvalues incorrect? Question: I am trying to get the eigenvalues of a positive semi-definite matrix in Python using numpy. All eigenvalues should be non-negative and real. The minimum eigenvalue should be zero. Sometimes, numpy.eig returns complex values as eigenvalues even when they’re supposed to be real (although the complex numbers have 0j in …

Total answers: 1

Multigrid Poisson Solver

Multigrid Poisson Solver Question: I am trying to make my own CFD solver and one of the most computationally expensive parts is solving for the pressure term. One way to solve Poisson differential equations faster is by using a multigrid method. The basic recursive algorithm for this is: function phi = V_Cycle(phi,f,h) % Recursive V-Cycle …

Total answers: 2

How to find where two lines intersect using numpy.linalg.solve given points coordinates?

How to find where two lines intersect using numpy.linalg.solve given points coordinates? Question: So I’m trying to use numpy.linalg.solve() to find where two lines intersect with each other using only some endpoints coordinates. If the coordinates of one lines are: (x1, y1), (x2, y2). I tried: import numpy as np a = np.array([[y2-y1],[x1-x2]]) b = …

Total answers: 2

How to calculate the rotation angles needed on X and Z to align two vectors

How to calculate the rotation angles needed on X and Z to align two vectors Question: I am trying to rotate vector1 (red) so that it aligns with vector2 (blue) in 3D space. However, only rotations around the X and Z axis should be used. So far, I have solved this using an optimizing algorithm …

Total answers: 1