Since: 1.0.0
Author: Gordon Lesti (info@gordonlesti.com)
Inheritance: extends NumPHP\Core\Cache
コード例 #1
0
ファイル: AbsTest.php プロジェクト: buaa556/gongkesheng
 /**
  * Tests if cache will be flushed after using NumArray::abs
  */
 public function testAbsCache()
 {
     $numArray = new NumArray(5);
     $numArray->setCache('key', 6);
     $numArray->abs();
     $this->assertFalse($numArray->inCache('key'));
 }
コード例 #2
0
ファイル: Reduce.php プロジェクト: buaa556/gongkesheng
 /**
  * Combines all elements of an NumArray at a given axis with the `$callback`
  * function
  *
  * @param NumArray $numArray given NumArray
  * @param callback $callback callback function
  * @param int      $axis     given axis
  *
  * @return NumArray
  *
  * @throws InvalidArgumentException will be thrown, if axis is out of bounds
  *
  * @since 1.0.0
  */
 public static function reduceArray(NumArray $numArray, $callback, $axis)
 {
     if ($axis && $axis >= $numArray->getNDim()) {
         throw new InvalidArgumentException('Axis ' . $axis . ' out of bounds');
     }
     return new NumArray(self::reduceRecursive($numArray->getData(), $callback, $axis));
 }
コード例 #3
0
ファイル: SubTest.php プロジェクト: raslin/NumPHP
 /**
  * Tests if cache will be flushed after use of NumArray::sub
  */
 public function testSubCache()
 {
     $numArray = new NumArray(5);
     $numArray->setCache('key', 7);
     $numArray->sub(3);
     $this->assertFalse($numArray->inCache('key'));
 }
コード例 #4
0
ファイル: NumArrayTest.php プロジェクト: buaa556/gongkesheng
 /**
  * Tests NumArray::getNDim
  */
 public function testNDim()
 {
     $numArray = new NumArray(1);
     $this->assertSame(0, $numArray->getNDim());
     $numArray = NumPHP::arange(1, 2);
     $this->assertSame(1, $numArray->getNDim());
     $numArray = NumPHP::arange(1, 6)->reshape(2, 3);
     $this->assertSame(2, $numArray->getNDim());
 }
コード例 #5
0
ファイル: Cholesky.php プロジェクト: buaa556/gongkesheng
 /**
  * @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>');
 }
コード例 #6
0
ファイル: Inverse.php プロジェクト: raslin/NumPHP
 /**
  * @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>');
 }
コード例 #7
0
ファイル: LUDecomposition.php プロジェクト: raslin/NumPHP
 /**
  * @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>');
 }
コード例 #8
0
ファイル: CacheTest.php プロジェクト: raslin/NumPHP
 /**
  * Tests if CacheKeyException will be thrown, when using Cache::flushCache and
  * key is not a string
  *
  * @expectedException        \NumPHP\Core\Exception\CacheKeyException
  * @expectedExceptionMessage Key has to be a string
  */
 public function testFlushCacheForbiddenKey()
 {
     $numArray = new NumArray(6);
     $numArray->flushCache(6);
 }
コード例 #9
0
ファイル: LinAlg.php プロジェクト: buaa556/gongkesheng
 /**
  * Calculates the inverse of a not singular square matrix
  *
  * @param mixed $squareMatrix not singular matrix
  *
  * @throws SingularMatrixException will be thrown, when `$squareMatrix` is singular
  *
  * @api
  * @since 1.0.0
  *
  * @return NumArray
  */
 public static function inv($squareMatrix)
 {
     if (!$squareMatrix instanceof NumArray) {
         $squareMatrix = new NumArray($squareMatrix);
     } elseif ($squareMatrix->inCache(self::CACHE_KEY_INVERSE)) {
         return $squareMatrix->getCache(self::CACHE_KEY_INVERSE);
     }
     if (!Helper::isNotSingularMatrix($squareMatrix)) {
         throw new SingularMatrixException("Matrix is singular");
     }
     $shape = $squareMatrix->getShape();
     $inv = self::solve($squareMatrix, NumPHP::identity($shape[0]));
     $squareMatrix->setCache(self::CACHE_KEY_INVERSE, $inv);
     return self::inv($squareMatrix);
 }
コード例 #10
0
ファイル: Helper.php プロジェクト: buaa556/gongkesheng
 /**
  * Tests if a NumArray is a square matrix
  *
  * @param NumArray $numArray given NumArray
  *
  * @return bool
  *
  * @since 1.0.0
  */
 public static function isSquareMatrix(NumArray $numArray)
 {
     $shape = $numArray->getShape();
     return self::isMatrix($numArray) && $shape[0] === $shape[1];
 }
コード例 #11
0
 /**
  * Searches the pivot index in an array
  *
  * @param NumArray $numArray given array
  * @param int      $iIndex   index
  *
  * @return int
  *
  * @since 1.0.0
  */
 protected static function getPivotIndex(NumArray $numArray, $iIndex)
 {
     $shape = $numArray->getShape();
     $mAxis = $shape[0];
     $max = abs($numArray->get($iIndex, $iIndex)->getData());
     $maxIndex = $iIndex;
     for ($j = $iIndex + 1; $j < $mAxis; $j++) {
         $abs = abs($numArray->get($j, $iIndex)->getData());
         if ($abs > $max) {
             $max = $abs;
             $maxIndex = $j;
         }
     }
     return $maxIndex;
 }
コード例 #12
0
ファイル: NumArray.php プロジェクト: numphp/numphp
 /**
  * Multiplies an array, NumArray or numeric value to the existing NumArray
  *
  * @param mixed $factor an other int, float, array or NumArray
  *
  * @return $this
  *
  * @api
  * @since 1.0.0
  */
 public function dot($factor)
 {
     if (!$factor instanceof NumArray) {
         $factor = new NumArray($factor);
     }
     $result = Dot::dotArray($this->data, $this->shape, $factor->getData(), $factor->getShape());
     $this->data = $result['data'];
     $this->shape = $result['shape'];
     $this->flushCache();
     return $this;
 }
コード例 #13
0
ファイル: NumPHP.php プロジェクト: buaa556/gongkesheng
 /**
  * Returns a NumArray with the same size, but filled with random values
  *
  * @param NumArray $numArray given NumArray
  *
  * @return NumArray
  *
  * @api
  * @since 1.0.0
  */
 public static function randLike(NumArray $numArray)
 {
     return new NumArray(Generate::generateArray($numArray->getShape()));
 }
コード例 #14
0
 /**
  * @param NumArray $matrix
  * @param NumArray $vector
  * @return NumArray
  */
 protected static function backSubstitution(NumArray $matrix, NumArray $vector)
 {
     $shape = $matrix->getShape();
     $xVector = \NumPHP\Core\NumPHP::zeros($shape[0]);
     for ($i = $shape[0] - 1; $i >= 0; $i--) {
         $sum = 0;
         for ($j = $i + 1; $j < $shape[0]; $j++) {
             $sum += $matrix->get($i, $j)->dot($xVector->get($j))->getData();
         }
         $xVector->set($i, ($vector->get($i)->getData() - $sum) / $matrix->get($i, $i)->getData());
     }
     return $xVector;
 }
コード例 #15
0
ファイル: ShapeTest.php プロジェクト: buaa556/gongkesheng
 /**
  * Tests NumArray::reshape with matrix 3x4 to matrix 2x6
  */
 public function testReshape3x4To2x6()
 {
     $numArray = new NumArray([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]);
     $expectedNumArray = new NumArray([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]);
     $this->assertNumArrayEquals($expectedNumArray, $numArray->reshape(2, 6));
 }
コード例 #16
0
ファイル: MaxTest.php プロジェクト: buaa556/gongkesheng
 /**
  * Tests NumArray::max with a matrix and argument 1
  */
 public function testMaxMatrixAxis1()
 {
     $numArray = new NumArray([[354, 56, -78], [-1, 453, 67]]);
     $expectedNumArray = new NumArray([354, 453]);
     $this->assertNumArrayEquals($expectedNumArray, $numArray->max(1));
 }
コード例 #17
0
ファイル: GetTest.php プロジェクト: numphp/numphp
 /**
  * Tests NumArray::get with argument 0 on vector with size 1
  */
 public function testGet1Args0()
 {
     $numArray = new NumArray([1]);
     $expectedNumArray = new NumArray(1);
     $this->assertNumArrayEquals($expectedNumArray, $numArray->get(0));
 }
コード例 #18
0
ファイル: LinAlgTest.php プロジェクト: buaa556/gongkesheng
 /**
  * Tests LinAlg::inv cache
  */
 public function testInvCache()
 {
     $numArray = new NumArray(5);
     $numArray->setCache(LinAlg::CACHE_KEY_INVERSE, 8);
     $this->assertSame(8, LinAlg::inv($numArray));
 }
コード例 #19
0
ファイル: MinTest.php プロジェクト: raslin/NumPHP
 /**
  * Tests NumArray::min with a matrix and argument 1
  */
 public function testMinMatrixAxis1()
 {
     $numArray = new NumArray([[34, 346, -12], [56, -78, 12], [345, -6, 99]]);
     $expectedNumArray = new NumArray([-12, -78, -6]);
     $this->assertNumArrayEquals($expectedNumArray, $numArray->min(1));
 }
コード例 #20
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));
 }
コード例 #21
0
 public function testCholeskyCache()
 {
     $numArray = new NumArray(5);
     $numArray->setCache(LinAlg\CholeskyDecomposition::CACHE_KEY_CHOLESKY, 8);
     $this->assertSame(8, LinAlg::cholesky($numArray));
 }
コード例 #22
0
ファイル: TransposeTest.php プロジェクト: raslin/NumPHP
 /**
  * Tests caching of NumArray::getTranspose
  */
 public function testTransposeCache()
 {
     $numArray = new NumArray(5);
     $numArray->setCache(NumArray\Transpose::CACHE_KEY_TRANSPOSE, 8);
     $this->assertSame(8, $numArray->getTranspose());
 }
コード例 #23
0
ファイル: SumTest.php プロジェクト: numphp/numphp
 /**
  * Tests if InvalidArgumentException will be thrown by using NumArray::sum and a
  * wrong axis on a scalar value
  *
  * @expectedException        \NumPHP\Core\Exception\InvalidArgumentException
  * @expectedExceptionMessage Axis 1 out of bounds
  */
 public function testSumSingleAxis1()
 {
     $numArray = new NumArray(6);
     $numArray->sum(1);
 }
コード例 #24
0
ファイル: index.php プロジェクト: buaa556/gongkesheng
    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;
    }
コード例 #25
0
ファイル: MeanTest.php プロジェクト: buaa556/gongkesheng
 /**
  * Tests NumArray::mean with a matrix and argument 1
  */
 public function testMeanMatrixAxis1()
 {
     $numArray = new NumArray([[12, -43, 6], [-3, 89, 23]]);
     $expectedNumArray = new NumArray([-25 / 3, 109 / 3]);
     $this->assertNumArrayEquals($expectedNumArray, $numArray->mean(1));
 }
コード例 #26
0
ファイル: LinearSystem.php プロジェクト: buaa556/gongkesheng
 /**
  * Back Substitution solves a linear system with a upper triangular matrix of
  * size n*n and a vector of size n or a matrix of size n*m
  *
  * @param NumArray $uMatrix   upper triangular matrix of size n*n
  * @param NumArray $numArray  vector of size n or matrix of size n*m
  *
  * @return NumArray
  *
  * @since 1.0.0
  */
 protected static function backSubstitution(NumArray $uMatrix, NumArray $numArray)
 {
     $shape = $numArray->getShape();
     if (Helper::isVector($numArray)) {
         $xVector = NumPHP::zerosLike($numArray);
         for ($i = $shape[0] - 1; $i >= 0; $i--) {
             $slice = sprintf("%d:%d", $i + 1, $shape[0]);
             $sum = $uMatrix->get($i, $slice)->dot($xVector->get($slice));
             $xVector->set($i, $numArray->get($i)->sub($sum)->div($uMatrix->get($i, $i)));
         }
         return $xVector;
     }
     // $numArray is a matrix
     $copy = clone $numArray;
     for ($i = 0; $i < $shape[1]; $i++) {
         $copy->set(':', $i, self::backSubstitution($uMatrix, $copy->get(':', $i)));
     }
     return $copy;
 }