GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch dev (ec2832)
by t
03:09
created

MMAT::mround()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
/**
3
 * Trait MathAndTrigonometry-M
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
namespace icy2003\php\icomponents\excel\mathAndTrigonometry;
10
11
use icy2003\php\ihelpers\Arrays;
12
13
/**
14
 * MathAndTrigonometry-M
15
 */
16
trait MMAT
17
{
18
    /**
19
     * 返回一个数组的矩阵行列式的值
20
     *
21
     * - det(A) = ai1Ai1 + ... + ainAin
22
     *
23
     * @param array $array 必需。 行数和列数相等的数值数组
24
     *
25
     * @return double
26
     */
27
    public static function mdeterm($array)
28
    {
29
        $n = count($array);
30
        $sum = 0;
31
        $prod = 1;
32
        for ($i = 0; $i < $n - 1; $i++) {
33
            for ($j = 0; $j < $n; $j++) {
34
                $x = ($j + $i) % $n;
35
                $y = $j % $n;
36
                $prod *= $array[$x][$y];
37
            }
38
            $sum += $prod;
39
            $prod = 1;
40
            for ($j = $n - 1; $j >= 0; $j--) {
41
                $x = ($j + $i) % $n;
42
                $y = ($n - $j - 1) % $n;
43
                $prod *= $array[$x][$y];
44
            }
45
            $sum -= $prod;
46
            $prod = 1;
47
        }
48
        return $sum;
49
    }
50
51
    /**
52
     * 返回数组中存储的矩阵的逆矩阵
53
     *
54
     * @todo
55
     *
56
     * @return array
57
     */
58
    public static function minverse()
59
    {
60
        return [];
61
    }
62
63
    /**
64
     * 返回两个数组的矩阵乘积。 结果矩阵的行数与 array1 的行数相同,矩阵的列数与 array2 的列数相同
65
     *
66
     * - Aij = ∑(k = 1, n)Bik * Ckj,其中 i 为行数,j 为列数
67
     * - 矩阵乘积物理含义:
68
     *
69
     * | 买家\产品 | A | B | C |
70
     * | - | - | - | - |
71
     * | 甲 | 3 个 | 4 个 | 1 个 |
72
     * | 乙 | 4 个 | 1 个 | 2 个 |
73
     *
74
     * | 产品 | 价格 | 重量 |
75
     * | - | - | - |
76
     * | A | 5 元 | 10 斤 |
77
     * | B | 10 元 | 5 斤 |
78
     * | C | 9 元 | 6 斤 |
79
     *
80
     * | 买家 | 总花费 | 总重量 |
81
     * | - | - | - |
82
     * | 甲 | 64 | 56 |
83
     * | 乙 | 48 | 57 |
84
     *
85
     * @param array $array1
86
     * @param array $array2
87
     *
88
     * @return array|false
89
     */
90 1
    public static function mmult($array1, $array2)
91
    {
92 1
        list($col1, $row1) = Arrays::colRowCount($array1);
93 1
        list($col2, $row2) = Arrays::colRowCount($array2);
94 1
        if ($col1 === $row2 && $row1 === $col2) {
95 1
            $arr = [];
96 1
            for ($r = 0; $r < $row1; $r++) {
97 1
                for ($c = 0; $c < $col2; $c++) {
98
                    $arr[$r][$c] = array_sum(array_map(function ($a1, $a2) {
99 1
                        return $a1 * $a2;
100 1
                    }, $array1[$r], Arrays::column($array2, $c)));
101
                }
102
            }
103 1
            return $arr;
104
        } else {
105 1
            return false;
106
        }
107
    }
108
109
    /**
110
     * 返回两数相除的余数
111
     *
112
     * -  结果的符号与除数相同
113
     *
114
     * @param integer $number 必需。 要计算余数的被除数
115
     * @param integer $divisor 必需。 除数
116
     *
117
     * @return integer
118
     */
119 1
    public static function mod($number, $divisor)
120
    {
121 1
        return self::sign($divisor) * self::abs($number % $divisor);
122
    }
123
124
    /**
125
     * 返回舍入到所需倍数的数字
126
     *
127
     * @param double $number 必需。 要舍入的值
128
     * @param double $multiple 必需。 要舍入到的倍数
129
     *
130
     * @return double|false
131
     */
132 1
    public static function mround($number, $multiple)
133
    {
134 1
        if ($number * $multiple < 0) {
135 1
            return false;
136
        }
137 1
        return round($number / $multiple) * $multiple;
138
    }
139
140
    /**
141
     * 返回参数和的阶乘与各参数阶乘乘积的比值
142
     *
143
     * @param double $number1 Number1 是必需的,后续数字是可选的
144
     *
145
     * @return double
146
     */
147 1
    public static function multinomial($number1)
148
    {
149 1
        $numbers = is_array($number1) ? $number1 : func_get_args();
150
        return self::fact(array_sum($numbers)) / array_product(array_map(function ($num) {
151 1
            return self::fact($num);
152 1
        }, $numbers));
153
    }
154
155
    /**
156
     * 返回指定维度的单位矩阵
157
     *
158
     * - 1NxN =
159
     * ```
160
     * 1 0 ... 0
161
     * 0 1 ... 0
162
     * ...   ...
163
     * 0 0 ... 1
164
     * ```
165
     * - 注:null 值在 php 里做运算时被看成 0,因此无需对 0 的元素进行赋值
166
     *
167
     * @param integer $dimension 必需。 Dimension 是一个整数, 指定要返回的单位矩阵的维度。 它返回一个数组。 维度必须大于零
168
     *
169
     * @return array
170
     */
171 1
    public static function munit($dimension)
172
    {
173 1
        $array = [];
174 1
        for ($i = 0; $i < $dimension; $i++) {
175 1
            $array[$i][$i] = 1;
176
        }
177 1
        return $array;
178
    }
179
}
180