本文整理汇总了PHP中square函数的典型用法代码示例。如果您正苦于以下问题:PHP square函数的具体用法?PHP square怎么用?PHP square使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了square函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getPlayerCovarianceMatrix
private static function getPlayerCovarianceMatrix(array &$teamAssignmentsList)
{
// This is a square matrix whose diagonal values represent the variance (square of standard deviation) of all
// players.
return new DiagonalMatrix(self::getPlayerRatingValues($teamAssignmentsList, function ($rating) {
return square($rating->getStandardDeviation());
}));
}
示例2: even_square
function even_square($arr)
{
foreach ($arr as $value) {
if ($value % 2 == 0) {
yield from square($value);
}
}
}
示例3: sumofsquares
function sumofsquares($n, $sum = 0)
{
if ($n == 0) {
return $sum;
} else {
$sum += square($n);
return sumofsquares($n - 1, $sum);
}
}
示例4: Mandelbrot
function Mandelbrot($x, $y)
{
$z = 0;
$c = array($x, $y);
for ($i = 0; $i < N; ++$i) {
$z = add(square($z), $c);
if (value($z) > M) {
return $i;
}
}
return true;
}
示例5: testDivision
public function testDivision()
{
// Since the multiplication was worked out by hand, we use the same numbers but work backwards
$product = new GaussianDistribution(0.2, 3.0 / sqrt(10));
$standardNormal = new GaussianDistribution(0, 1);
$productDividedByStandardNormal = GaussianDistribution::divide($product, $standardNormal);
$this->assertEquals(2.0, $productDividedByStandardNormal->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$this->assertEquals(3.0, $productDividedByStandardNormal->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$product2 = new GaussianDistribution((4 * square(7) + 6 * square(5)) / (square(5) + square(7)), sqrt(square(5) * square(7) / (square(5) + square(7))));
$m4s5 = new GaussianDistribution(4, 5);
$product2DividedByM4S5 = GaussianDistribution::divide($product2, $m4s5);
$this->assertEquals(6.0, $product2DividedByM4S5->getMean(), '', GaussianDistributionTest::ERROR_TOLERANCE);
$this->assertEquals(7.0, $product2DividedByM4S5->getStandardDeviation(), '', GaussianDistributionTest::ERROR_TOLERANCE);
}
示例6: similarly
function similarly($r, $g, $b, $color)
{
return 1 / (1 + sqrt(square($r - $color[0]) + square($g - $color[1]) + square($b - $color[2])));
}
示例7: displayResults
//.........这里部分代码省略.........
//5 = 5 Point Scale
//A = Array (5 Point Choice)
if ($outputs['qtype'] == "5" || $outputs['qtype'] == "A") {
$stddev = 0;
$stddevarray = array_slice($grawdata, 0, 5, true);
$am = 0;
//calculate arithmetic mean
if (isset($sumitems) && $sumitems > 0) {
//calculate and round results
//there are always 5 items
for ($x = 0; $x < 5; $x++) {
//create product of item * value
$am += ($x + 1) * $stddevarray[$x];
}
//prevent division by zero
if (isset($stddevarray) && array_sum($stddevarray) > 0) {
$am = round($am / array_sum($stddevarray), 2);
} else {
$am = 0;
}
//calculate standard deviation -> loop through all data
/*
* four steps to calculate the standard deviation
* 1 = calculate difference between item and arithmetic mean and multiply with the number of elements
* 2 = create sqaure value of difference
* 3 = sum up square values
* 4 = multiply result with 1 / (number of items)
* 5 = get root
*/
for ($j = 0; $j < 5; $j++) {
//1 = calculate difference between item and arithmetic mean
$diff = $j + 1 - $am;
//2 = create square value of difference
$squarevalue = square($diff);
//3 = sum up square values and multiply them with the occurence
//prevent divison by zero
if ($squarevalue != 0 && $stddevarray[$j] != 0) {
$stddev += $squarevalue * $stddevarray[$j];
}
}
//4 = multiply result with 1 / (number of items (=5))
//There are two different formulas to calculate standard derivation
//$stddev = $stddev / array_sum($stddevarray); //formula source: http://de.wikipedia.org/wiki/Standardabweichung
//prevent division by zero
if (array_sum($stddevarray) - 1 != 0 && $stddev != 0) {
$stddev = $stddev / (array_sum($stddevarray) - 1);
//formula source: http://de.wikipedia.org/wiki/Empirische_Varianz
} else {
$stddev = 0;
}
//5 = get root
$stddev = sqrt($stddev);
$stddev = round($stddev, 2);
}
switch ($outputType) {
case 'xls':
$this->xlsRow++;
$this->sheet->write($this->xlsRow, 0, gT("Arithmetic mean"));
$this->sheet->writeNumber($this->xlsRow, 1, $am);
$this->xlsRow++;
$this->sheet->write($this->xlsRow, 0, gT("Standard deviation"));
$this->sheet->writeNumber($this->xlsRow, 1, $stddev);
break;
case 'pdf':
$tablePDF[] = array(gT("Arithmetic mean"), $am, '', '');
$tablePDF[] = array(gT("Standard deviation"), $stddev, '', '');
示例8: calculateMatchQuality
/**
* {@inheritdoc }
*/
public function calculateMatchQuality(GameInfo &$gameInfo, array &$teams)
{
Guard::argumentNotNull($gameInfo, "gameInfo");
$this->validateTeamCountAndPlayersCountPerTeam($teams);
// We've verified that there's just two teams
$team1Ratings = $teams[0]->getAllRatings();
$team1Count = count($team1Ratings);
$team2Ratings = $teams[1]->getAllRatings();
$team2Count = count($team2Ratings);
$totalPlayers = $team1Count + $team2Count;
$betaSquared = square($gameInfo->getBeta());
$meanGetter = function ($currentRating) {
return $currentRating->getMean();
};
$varianceGetter = function ($currentRating) {
return square($currentRating->getStandardDeviation());
};
$team1MeanSum = sum($team1Ratings, $meanGetter);
$team1StdDevSquared = sum($team1Ratings, $varianceGetter);
$team2MeanSum = sum($team2Ratings, $meanGetter);
$team2SigmaSquared = sum($team2Ratings, $varianceGetter);
// This comes from equation 4.1 in the TrueSkill paper on page 8
// The equation was broken up into the part under the square root sign and
// the exponential part to make the code easier to read.
$sqrtPart = sqrt($totalPlayers * $betaSquared / ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared));
$expPart = exp(-1 * square($team1MeanSum - $team2MeanSum) / (2 * ($totalPlayers * $betaSquared + $team1StdDevSquared + $team2SigmaSquared)));
return $expPart * $sqrtPart;
}
示例9: generate_statistics
//.........这里部分代码省略.........
{
//create product of item * value
$am += (($x+1) * $stddevarray[$x]);
}
//prevent division by zero
if(isset($stddevarray) && array_sum($stddevarray) > 0)
{
$am = round($am / array_sum($stddevarray),2);
}
else
{
$am = 0;
}
//calculate standard deviation -> loop through all data
/*
* four steps to calculate the standard deviation
* 1 = calculate difference between item and arithmetic mean and multiply with the number of elements
* 2 = create sqaure value of difference
* 3 = sum up square values
* 4 = multiply result with 1 / (number of items)
* 5 = get root
*/
for($j = 0; $j < 5; $j++)
{
//1 = calculate difference between item and arithmetic mean
$diff = (($j+1) - $am);
//2 = create square value of difference
$squarevalue = square($diff);
//3 = sum up square values and multiply them with the occurence
//prevent divison by zero
if($squarevalue != 0 && $stddevarray[$j] != 0)
{
$stddev += $squarevalue * $stddevarray[$j];
}
}
//4 = multiply result with 1 / (number of items (=5))
//There are two different formulas to calculate standard derivation
//$stddev = $stddev / array_sum($stddevarray); //formula source: http://de.wikipedia.org/wiki/Standardabweichung
//prevent division by zero
if((array_sum($stddevarray)-1) != 0 && $stddev != 0)
{
$stddev = $stddev / (array_sum($stddevarray)-1); //formula source: http://de.wikipedia.org/wiki/Empirische_Varianz
}
else
{
$stddev = 0;
}
//5 = get root
$stddev = sqrt($stddev);
$stddev = round($stddev,2);
}
switch($outputType)
{
case 'xls':
示例10: displayResults
//.........这里部分代码省略.........
//5 = 5 Point Scale
//A = Array (5 Point Choice)
if ($outputs['qtype'] == "5" || $outputs['qtype'] == "A") {
$stddev = 0;
$stddevarray = array_slice($grawdata, 0, 5, true);
$am = 0;
//calculate arithmetic mean
if (isset($sumitems) && $sumitems > 0) {
//calculate and round results
//there are always 5 items
for ($x = 0; $x < 5; $x++) {
//create product of item * value
$am += ($x + 1) * $stddevarray[$x];
}
//prevent division by zero
if (isset($stddevarray) && array_sum($stddevarray) > 0) {
$am = round($am / array_sum($stddevarray), 2);
} else {
$am = 0;
}
//calculate standard deviation -> loop through all data
/*
* four steps to calculate the standard deviation
* 1 = calculate difference between item and arithmetic mean and multiply with the number of elements
* 2 = create sqaure value of difference
* 3 = sum up square values
* 4 = multiply result with 1 / (number of items)
* 5 = get root
*/
for ($j = 0; $j < 5; $j++) {
//1 = calculate difference between item and arithmetic mean
$diff = $j + 1 - $am;
//2 = create square value of difference
$squarevalue = square($diff);
//3 = sum up square values and multiply them with the occurence
//prevent divison by zero
if ($squarevalue != 0 && $stddevarray[$j] != 0) {
$stddev += $squarevalue * $stddevarray[$j];
}
}
//4 = multiply result with 1 / (number of items (=5))
//There are two different formulas to calculate standard derivation
//$stddev = $stddev / array_sum($stddevarray); //formula source: http://de.wikipedia.org/wiki/Standardabweichung
//prevent division by zero
if (array_sum($stddevarray) - 1 != 0 && $stddev != 0) {
$stddev = $stddev / (array_sum($stddevarray) - 1);
//formula source: http://de.wikipedia.org/wiki/Empirische_Varianz
} else {
$stddev = 0;
}
//5 = get root
$stddev = sqrt($stddev);
$stddev = round($stddev, 2);
}
switch ($outputType) {
case 'xls':
$this->xlsRow++;
$this->sheet->write($this->xlsRow, 0, gT("Arithmetic mean"));
$this->sheet->writeNumber($this->xlsRow, 1, $am);
$this->xlsRow++;
$this->sheet->write($this->xlsRow, 0, gT("Standard deviation"));
$this->sheet->writeNumber($this->xlsRow, 1, $stddev);
break;
case 'pdf':
$tablePDF[] = array(gT("Arithmetic mean"), $am, '', '');
$tablePDF[] = array(gT("Standard deviation"), $stddev, '', '');
示例11: displayResults
//.........这里部分代码省略.........
//5 = 5 Point Scale
//A = Array (5 Point Choice)
if ($outputs['qtype'] == "5" || $outputs['qtype'] == "A") {
$stddev = 0;
$stddevarray = array_slice($grawdata, 0, 5, true);
$am = 0;
//calculate arithmetic mean
if (isset($sumitems) && $sumitems > 0) {
//calculate and round results
//there are always 5 items
for ($x = 0; $x < 5; $x++) {
//create product of item * value
$am += ($x + 1) * $stddevarray[$x];
}
//prevent division by zero
if (isset($stddevarray) && array_sum($stddevarray) > 0) {
$am = round($am / array_sum($stddevarray), 2);
} else {
$am = 0;
}
//calculate standard deviation -> loop through all data
/*
* four steps to calculate the standard deviation
* 1 = calculate difference between item and arithmetic mean and multiply with the number of elements
* 2 = create sqaure value of difference
* 3 = sum up square values
* 4 = multiply result with 1 / (number of items)
* 5 = get root
*/
for ($j = 0; $j < 5; $j++) {
//1 = calculate difference between item and arithmetic mean
$diff = $j + 1 - $am;
//2 = create square value of difference
$squarevalue = square($diff);
//3 = sum up square values and multiply them with the occurence
//prevent divison by zero
if ($squarevalue != 0 && $stddevarray[$j] != 0) {
$stddev += $squarevalue * $stddevarray[$j];
}
}
//4 = multiply result with 1 / (number of items (=5))
//There are two different formulas to calculate standard derivation
//$stddev = $stddev / array_sum($stddevarray); //formula source: http://de.wikipedia.org/wiki/Standardabweichung
//prevent division by zero
if (array_sum($stddevarray) - 1 != 0 && $stddev != 0) {
$stddev = $stddev / (array_sum($stddevarray) - 1);
//formula source: http://de.wikipedia.org/wiki/Empirische_Varianz
} else {
$stddev = 0;
}
//5 = get root
$stddev = sqrt($stddev);
$stddev = round($stddev, 2);
}
switch ($outputType) {
case 'xls':
$this->xlsRow++;
$this->sheet->write($this->xlsRow, 0, gT("Arithmetic mean"));
$this->sheet->writeNumber($this->xlsRow, 1, $am);
$this->xlsRow++;
$this->sheet->write($this->xlsRow, 0, gT("Standard deviation"));
$this->sheet->writeNumber($this->xlsRow, 1, $stddev);
break;
case 'pdf':
$tablePDF[] = array(gT("Arithmetic mean"), $am, '', '');
$tablePDF[] = array(gT("Standard deviation"), $stddev, '', '');
示例12: switch
$age = 7;
switch (true) {
case $age < 5:
// no school for you! *snap*
break;
case $age > 4 && $age < 13:
// grade school
break;
case $age > 12 && $age < 18:
// high school
break;
case $age > 17:
// adult ed
break;
default:
// ??? get a job then
break;
}
$a = 10;
function foo()
{
$a = 5;
}
function square(&$val)
{
$val = $val * $val;
return $val;
}
echo '<p>' . $a . '</p>';
echo '<p>' . square($a) . '</p>';
echo '<p>' . $a . '</p>';
示例13: square
// Step 1. Do the following drills & exercises in PHP.
// Step 2. Commit your answers to github and send me the link. Call the file "drills.php" or something similar.
// Step 3. Don't forget to go into `vagrant ssh` and do `php -a` if you need a PHP console for rapid testing out of ideas.
// Step 4. Don't forget to use PHP's internal functions. They make your tasks easier!
// Step 5. If you're stuck, Google the heck out of stuff and search StackOverflow for solutions.
// Write a function that takes in a number and returns the number multiplied by itself.
// Make a function that takes in a string and returns it capitalized. If the input is not a string, return false.
// Make a function that takes in a string and returns it lowercased. If the input is not a string, return false.
// Write a function called isVowel that takes in a single character and returns true if the character is a vowel, false if not.
// Write a function called countVowels that takes in a string and returns the number of vowels in the string.
function square($number)
{
$square = $number * $number;
return $square;
}
echo square(7);
//
function upperCase($string)
{
if (!is_string($string)) {
return false;
}
}
return strtoupper($string);
echo upperCase([]);
echo upperCase("foo");
//
function lowerCase($string)
{
if (!is_string($string)) {
return false;
示例14: foreach
}
?>
<style type="text/css">
.square {
margin:2px;
float:left;
text-align:center;
position:relative;
height:50px;
width:50px;
font-size:6pt;
font-family:Arial, Verdana, sans-serif;
}
</style>
<?php
echo '<table border="0" cellpadding="8" cellspacing="0"><tr valign="top"><td id="palette" style="width:312px;">';
foreach ($theme as $t) {
square($t);
}
shuffle($theme);
foreach ($theme as $t) {
square($t);
}
echo '</td>';
echo '<td>';
echo '<div style="height:800px; overflow:scroll">';
foreach ($rgb_table as $t => $nop) {
square($t);
}
echo '</div>';
echo '</td></tr></table>';
示例15: soma
static $total;
$total += $km;
return $km;
}
// print($total);
print $line;
// ----------------------------------------------------------
//imprimir diretamente o valor retornado
print soma(2, 4, 6);
function soma($num1, $num2, $num3)
{
return $num1 + $num2 + $num3;
}
print $line;
// ----------------------------------------------------------
print square(4);
print $line;
// ----------------------------------------------------------
//TRABALHANDO COM ARRAYS!
//Uma maneira de montar um array simples
$vet = array('maça', 'laranja', 'banana', 'pera', 'abacaxi');
//Segunda maneira de montar array ASSOCIATIVO, onde informo a chave e o seu valor
$vet2 = array("maça" => "vermelha", "laranja" => "Adivinha", "banana" => 3, "pera" => 4, "abacaxi" => 5, "abacate" => "verde com caroço");
//Terceira maneira de montar um array simples
$vet3[] = "Maria";
$vet3[] = "José";
$vet3[] = "Ricardo";
$vet3[] = "Jucelina";
$vet3[] = "Astrogildo";
//Quarta maneira de montar um vetor em formato ASSOCIATIVO
$vet4["nome"] = "Maria da Silva";