Пример #1
0
 /**
  * Solves a linear system
  *
  * @param NumArray $squareMatrix matrix of size n*n
  * @param NumArray $numArray     vector of size n or matrix of size n*m
  *
  * @throws \NumPHP\LinAlg\Exception\SingularMatrixException will be thrown, if
  * `$squareMatrix` is singular
  * @throws \NumPHP\LinAlg\Exception\InvalidArgumentException will be thrown, if
  * linear system of `$squareMatrix` and `$numArray` can not be solved
  *
  * @return NumArray
  *
  * @since 1.0.0
  */
 public static function solve(NumArray $squareMatrix, NumArray $numArray)
 {
     if (!Helper::isNotSingularMatrix($squareMatrix)) {
         throw new SingularMatrixException(sprintf("First Argument has to be a not singular square matrix"));
     }
     if (!Helper::isVector($numArray) && !Helper::isMatrix($numArray)) {
         throw new InvalidArgumentException(sprintf("Second argument has to be a vector or a matrix, NumArray with dimension %d given", $numArray->getNDim()));
     }
     $shape1 = $squareMatrix->getShape();
     $shape2 = $numArray->getShape();
     if ($shape1[0] !== $shape2[0]) {
         throw new InvalidArgumentException(sprintf("Can not solve a linear system with matrix (%s) and matrix (%s)", implode(', ', $shape1), implode(', ', $shape2)));
     }
     /**
      * The result of LinAlg::lud is a array with three NumArrays
      *
      * @var NumArray $pMatrix
      * @var NumArray $lMatrix
      * @var NumArray $uMatrix
      */
     list($pMatrix, $lMatrix, $uMatrix) = LinAlg::lud($squareMatrix);
     $yNumArray = self::forwardSubstitution($lMatrix, $pMatrix->getTranspose()->dot($numArray));
     $zNumArray = self::backSubstitution($uMatrix, $yNumArray);
     return $zNumArray;
 }
Пример #2
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $matrixA = new NumArray([[49, -525, 7, -315], [-525, 6921, -3279, 3483], [7, -3279, 8178, -328], [-315, 3483, -328, 624556]]);
     $output->writeln('<comment>Matrix A:</comment>');
     $output->writeln($matrixA->__toString());
     $output->writeln('<info>Cholesky:</info>');
     $time = microtime(true);
     $matrixL = LinAlg::cholesky($matrixA);
     $timeDiff = microtime(true) - $time;
     $output->writeln($matrixL->__toString());
     $output->writeln('<info>Time for calculation: ' . $timeDiff . ' sec</info>');
 }
Пример #3
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $matrix = new NumArray([[-3, 4, 7 / 6], [2, 0.1, 0], [23, -5, 8]]);
     $output->writeln('<comment>Matrix:</comment>');
     $output->writeln($matrix->__toString());
     $output->writeln('<info>Inverse:</info>');
     $time = microtime(true);
     $inv = LinAlg::inv($matrix);
     $timeDiff = microtime(true) - $time;
     $output->writeln($inv->__toString());
     $output->writeln('<info>Time for calculation: ' . $timeDiff . ' sec</info>');
 }
Пример #4
0
 /**
  * @param InputInterface $input
  * @param OutputInterface $output
  *
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $matrixA = new NumArray([[1, 6, 1], [2, 3, 2], [4, 2, 1]]);
     $output->writeln('<comment>Matrix A:</comment>');
     $output->writeln($matrixA->__toString());
     $output->writeln('<info>LU decomposition</info>');
     $time = microtime(true);
     list($matrixP, $matrixL, $matrixU) = LinAlg::lud($matrixA);
     $timeDiff = microtime(true) - $time;
     $output->writeln('<comment>Matrix P:</comment>');
     $output->writeln($matrixP->__toString());
     $output->writeln('<comment>Matrix L:</comment>');
     $output->writeln($matrixL->__toString());
     $output->writeln('<comment>Matrix U:</comment>');
     $output->writeln($matrixU->__toString());
     $output->writeln('<info>Time for calculation: ' . $timeDiff . ' sec</info>');
 }
 public function testCholeskyCache()
 {
     $numArray = new NumArray(5);
     $numArray->setCache(LinAlg\CholeskyDecomposition::CACHE_KEY_CHOLESKY, 8);
     $this->assertSame(8, LinAlg::cholesky($numArray));
 }
Пример #6
0
 /**
  * Tests if a matrix is not singular
  *
  * @param NumArray $numArray given NumArray
  *
  * @todo better use matrix_rank to check if a matrix is singular
  *
  * @return bool
  *
  * @since 1.0.0
  */
 public static function isNotSingularMatrix(NumArray $numArray)
 {
     return self::isSquareMatrix($numArray) && LinAlg::det($numArray) != 0;
 }
Пример #7
0
 /**
  * Tests if InvalidArgumentException will be thrown, when using LinAlg::solve with not align matrix and vector
  *
  * @expectedException        \NumPHP\LinAlg\Exception\InvalidArgumentException
  * @expectedExceptionMessage Can not solve a linear system with matrix (4, 4) and matrix (3)
  */
 public function testSolveNotAlign()
 {
     $matrix = NumPHP::identity(4);
     $vector = NumPHP::ones(3);
     LinAlg::solve($matrix, $vector);
 }
Пример #8
0
    private function receiveText($object)
    {
        $keyword_raw = trim($object->Content);
        $str_key = mb_substr($keyword_raw, 0, 2, "UTF-8");
        $keyword = mb_substr($keyword_raw, 2, mb_strlen($keyword_raw) - 2, "UTF-8");
        if ($keyword_raw == "博主真帅") {
            $result = $this->transmitText($object, "谢谢夸奖。。不过我好像记错了。。。回复“帮助”查看帮助。。");
            return $result;
        }
        if ($keyword_raw == "主页君照片") {
            $result = $this->transmitText($object, '<a href="http://pic4.nipic.com/20091206/3688710_225859078487_2.jpg">点此查看主页君照片</a>');
            return $result;
        }
        if ($keyword_raw == "关于平台") {
            $content = json_decode('[{
								"Title": "关于平台",
								"Description": "关于本平台的一切",
								"PicUrl": "http://mmbiz.qpic.cn/mmbiz/9p8h2H28HOmAbjeSichfxCpLZt5Y4icvaicuwwf6JCXkVKvhVhVveuad1q4WD2FMOALOrCJNHQ2wA7unGvFpZufRw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1",
								"Url": "http://mp.weixin.qq.com/s?__biz=MzA3NDY5MDQ0OQ==&mid=401351136&idx=1&sn=2dbcbde7a50df32f5af8917c703d5d48&scene=4#wechat_redirect"
							}]', true);
            $result = $this->transmitNews($object, $content);
            return $result;
        }
        if ($keyword_raw == "帮助") {
            $result = $this->transmitText($object, "回复天气+城市名(如“天气北京”)查看本地天气预报\n回复翻译+词汇(如“翻译你好”)查看中译英\n回复英文单词(如“hello”)查看英译中(注:若有标点,请在前加翻译二字。)\n回复“关于平台”查看本平台详细信息\n回复“新闻”查看新闻\n回复菜名,如“鱼香肉丝”查看菜谱\n普通回复,将会召唤出图灵机器人与您聊天。\n回复邮件+内容(如“邮件主页君你好帅”)为主页君发送邮件,最好在邮件内容中附上你的邮箱方便主页君尽快联系您。");
            return $result;
        }
        if ($str_key == '计算') {
            if (strstr($keyword, "[")) {
                $input = explode("#", $keyword);
                if (sizeof($input) == 2) {
                    eval('$array1 = ' . $input[0] . ';');
                    $matrix1 = new NumArray($array1);
                    if (trim($input[1]) == "inv") {
                        $matrix1 = LinAlg::inv($matrix1);
                    }
                    if (trim($input[1]) == "det") {
                        $matrix1 = LinAlg::det($matrix1);
                    }
                } elseif (sizeof($input) == 3) {
                    eval('$array1 = ' . $input[0] . ';');
                    eval('$array2 = ' . $input[2] . ';');
                    $matrix1 = new NumArray($array1);
                    $matrix2 = new NumArray($array2);
                    if (trim($input[1]) == "+") {
                        $matrix1->add($matrix2);
                    }
                    if (trim($input[1]) == "-") {
                        $matrix1->sub($matrix2);
                    }
                    if (trim($input[1]) == "*") {
                        $matrix1->dot($matrix2);
                    }
                }
            } else {
                eval('$matrix1 = ' . $keyword . ';');
            }
            $output = (string) $matrix1;
            if ($matrix1 == 0) {
                $output == "0.000000000001";
            }
            $result = $this->transmitText($object, $output);
            return $result;
        }
        if ($str_key == "天气") {
            $url = "http://apix.sinaapp.com/weather/?appkey=" . $object->ToUserName . "&city=" . urlencode($keyword);
            $output = file_get_contents($url);
            $content = json_decode($output, true);
            $result = $this->transmitNews($object, $content);
            return $result;
        }
        if ($str_key == "邮件") {
            //引入发送邮件类
            require "smtp.php";
            //使用163邮箱服务器
            $smtpserver = "smtp.163.com";
            //163邮箱服务器端口
            $smtpserverport = 25;
            //你的163服务器邮箱账号
            $smtpusermail = "*****@*****.**";
            //收件人邮箱
            $smtpemailto = "*****@*****.**";
            //你的邮箱账号(去掉@163.com)
            $smtpuser = "******";
            //SMTP服务器的用户帐号
            //你的邮箱密码
            $smtppass = "";
            //SMTP服务器的用户密码
            //邮件主题
            $mailsubject = date("m-d") . "意见反馈";
            //邮件内容
            $mailbody = $keyword;
            //邮件格式(HTML/TXT),TXT为文本邮件
            $mailtype = "TXT";
            //这里面的一个true是表示使用身份验证,否则不使用身份验证.
            $smtp = new smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass);
            //是否显示发送的调试信息
            $smtp->debug = TRUE;
            //发送邮件
            $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
            $result = $this->transmitText($object, "成功发送邮件!");
            return $result;
        }
        if ($str_key == "翻译") {
            $content = $this->baiduDic($keyword);
            $result = $this->transmitText($object, $content);
            return $result;
        }
        if (preg_match("/^[a-zA-Z\\s]+\$/", $keyword_raw)) {
            $content = $this->baiduDic($keyword_raw);
            $result = $this->transmitText($object, $content);
            return $result;
        }
        $content = $this->tulingRobot($keyword_raw);
        if (!($content == "")) {
            $result = $this->transmitText($object, $content);
        } else {
            $output = $this->tulingRobotNews($keyword_raw);
            $temp = json_encode(array_slice($output, 0, 4));
            $final = json_decode($temp, true);
            $subtemp = mb_substr($temp, 0, 1022, "utf-8");
            $result = $this->transmitNews($object, $final);
        }
        return $result;
    }
Пример #9
0
 /**
  * Tests if SingularMatrixException will be thrown, when using LinAlg::inv with singular matrix
  *
  * @expectedException        \NumPHP\LinAlg\Exception\SingularMatrixException
  * @expectedExceptionMessage Matrix is singular
  */
 public function testInvSingular()
 {
     $matrix = new NumArray([[0]]);
     LinAlg::inv($matrix);
 }
Пример #10
0
 /**
  * Tests cache of LinAlg::lud
  */
 public function testLUDecompositionCache()
 {
     $numArray = new NumArray(5);
     $numArray->setCache(LUDecomposition::CACHE_KEY_LU_DECOMPOSITION, 8);
     $this->assertSame(8, LinAlg::lud($numArray));
 }