计算 Python 中的矩阵的2-范数,不使用 NumPy。

huangapple go评论109阅读模式
英文:

Calculate the 2-norm of a matrix in python without NumPy

问题

I'm trying to calculate the 2-norm of a given matrix without using NumPy.

A = ([[1, 2, 3, 4], 
    [5, 6, 100, 8],
    [9, 200, 11, 12]])

def norm2(A):
    f = 0

    for i in A:
        for j in range(0,len(A[0]))
          f = f + sum((abs(A[i, j]))^2)
    return(sqrt(f))

print(norm2(A))

But I can't understand why it is not working.

英文:

I'm trying to calculate the 2-norm of a given matrix without using NumPy.

A = ([[1, 2, 3, 4], 
    [5, 6, 100, 8],
    [9, 200, 11, 12]])

def norm2(A):
    f = 0

    for i in A:
        for j in range(0,len(A[0]))
          f = f + sum((abs(A[i, j]))^2)
    return(sqrt(f))

print(norm2(A))

But I can't understand why it is not workings

答案1

得分: 1

以下是要翻译的内容:

  1. 在内部循环中,您试图使用索引 i 和 j 访问矩阵 A 的元素。但是,i 是 A 的一行,不是索引,所以您应该只使用 i 而不是 A[i]。此外,您应该使用 j 来访问 A[i] 的元素,而不是使用不是 Python 中的有效语法的 A[i, j]。而是使用 A[i][j]。

  2. 您正在使用 ^ 运算符来计算矩阵中每个元素的平方。但是,在 Python 中,^ 是按位异或运算符,而不是指数运算符。要计算一个数字的平方,您可以使用 ** 运算符。

  3. 在内部循环中的 range 函数调用末尾缺少一个冒号(:)。没有冒号,Python 将引发语法错误。

以下是修改后的代码:

from math import sqrt

A = [[1, 2, 3, 4], 
    [5, 6, 100, 8],
    [9, 200, 11, 12]]

def norm2(A):
    f = 0

    for i in A:
        for j in range(0, len(A[0])):
            f = f + abs(A[i][j])**2

    return sqrt(f)

print(norm2(A))

经过这些修改,您的代码现在应该能够正确计算给定矩阵的2-范数。

英文:

There are a few issues in your code that are causing it to not work as expected. Here are some modifications you can make to fix them:

  1. In the inner for loop, you are trying to access elements of the matrix A using indices i and j. However, i is a row of A, not an index, so you should just use i instead of A[i]. Also, you should use j to access the elements of A[i] instead of using A[i, j] which is not valid syntax in Python. Instead, use A[i][j].

  2. You are using the ^ operator to calculate the square of each element of the matrix. However, in Python, ^ is the bitwise XOR operator, not the exponentiation operator. To calculate the square of a number, you can use the ** operator.

  3. You are missing a colon (:) at the end of the range function call in the inner loop. Without the colon, Python will raise a syntax error.

Here is the modified code:
from math import sqrt

A = [[1, 2, 3, 4], 
    [5, 6, 100, 8],
    [9, 200, 11, 12]]

def norm2(A):
    f = 0

    for i in A:
        for j in range(0, len(A[0])):
            f = f + abs(A[i][j])**2

    return sqrt(f)

print(norm2(A))

With these modifications, your code should now calculate the 2-norm of the given matrix correctly.

答案2

得分: 0

total = sum(value ** 2 for row in A for value in row)
return sqrt(total)
英文:

If you really wanted this to look like Python:

total = sum(value ** 2 for row in A for value in row)
return sqrt(total)

huangapple
  • 本文由 发表于 2023年3月7日 02:01:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定