【问题标题】:Efficient method to lower Matrix order in PHPPHP中降低矩阵顺序的有效方法
【发布时间】:2019-08-21 14:42:55
【问题描述】:

我正在用 PHP 开发一个行列式计算器

在 Matrix 类中,我创建了一些函数,其中包括函数 calc (order, matrix)。 该函数调用另一个函数将数组的顺序降低到$ order = 3,然后执行sarrus($matriz)函数。

注意:这个矩阵变量会随着顺序的降低而改变,即原来的矩阵会保存在另一个变量中!

好吧,我想知道将数组顺序降低到 3 的最佳方法,如果可能的话,我尝试使用 laplace 代码示例,但我在决定放弃一段时间的循环中感到很困惑。

public function calc ($order, $matriz)
{
    // If the order equals 1: the element is the determinant
    if ($order == 1) {
        $ this->det = $matriz[0][0];
    }
    // If the order equals 2: call the @segOrder function
    else if ($order == 2) {
        segOrder($matriz);
    }
    // If the order is 3: call the function @sarrus
    else if ($order == 3) {
        sarrus($matriz);
    }
    // If the order is greater than 3: call the function @leaveOrder to lower the array order to 3 and then use @sarrus to have the determinant
    else if ($order > 3) {
        $matriz = lowerOrder($matriz, $order);
        sarrus($matriz);
    }
    return $this->det;
}

数组布局:

$matriz = array (
            array (1,2,3),
            array (4,5,6),
            array (7,8,9)
          );

【问题讨论】:

    标签: php performance matrix determinants


    【解决方案1】:

    我想你是在要求这样的东西......

    /**
     * Truncates rows and columns from an array of arrays (ignoring rows/columns beyond a certain index).
     *
     * The returned value will have numeric indexes regardless of what you pass in.
     *
     * @param array $matrix
     * @param int $row_cap
     * @param int $col_cap
     * @return array
     */
    function matrix_slice( array $matrix, $row_cap = 3, $col_cap = 3 ) {
        return array_map( function( $vector ) use( $col_cap ){
            return array_slice( array_values( $vector ), 0, $col_cap );
        }, array_slice( array_values( $matrix ), 0, $row_cap ) );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多