is there any function in PHP which would get the cotangent and arccotangent of my number?
Thanks
And arccotangent(a) would be atan(1/a).
Take a look at the tan function in the PHP manual for the cotangent, notably the comment here for cotangents
<?php
//FOR PHP 4
function cot($rad)
{
return tan(M_PI_2 - rad2deg($rad));
}
//FOR PHP 3
function cot($rad)
{
return tan(M_PI/2 - rad2deg($rad));
}
?>
See here for #2
http://php.net/manual/en/function.atan.php
theres a cotan funciton in teh comments for http://php.net/manual/en/function.tan.php
I believe there are no built-ins for those. So you'll have to write/find some. Cot is in the comments for tan() on php.net
http://php.net/manual/en/function.tan.php
tan will give you the tangent. Cotangent is 1/tan. You can easily use the arctan function to calculate the arccotan taking in mind that
arccot x = (PI / 2) - arctan x
Related
I'm trying to cacluate the variance of an array in PHP and I can' use built in "stats_variance" function.
I've searched a lot and found a few functions in php to calculate Variance But the problem is that the number that "VAR.P" excel functions calculates is way different in php.
This is my Array of data :
1153680
1118118
1118912
1116187
1068844
1028538
1028538
988121
970411
973495
938557
938550
926133
959840
959841
986759
986759
1002125
995177
968461
987475
1017662
1021445
1047763
1043253
1020218
977923
977923
980571
979106
956817
917406
878101
878094
845952
820505
797824
769385
741932
741902
726137
708995
690126
668906
645763
645723
626389
624527
623260
607855
597254
597069
583344
569674
573903
567532
547658
545661
532522
521267
520108
508879
512900
512903
502077
505943
494479
502694
520211
520210
520059
535684
549185
555167
555165
543926
529170
515452
505676
523850
524394
517911
503306
498856
480875
478755
478754
472119
476812
472749
461988
459079
447151
454618
452842
445894
440199
438880
428216
426184
427139
420668
418312
417001
411211
406008
409807
410694
409962
399445
395912
392038
375650
359807
353807
358843
365815
367334
384099
377012
375547
369729
367632
361088
362259
359365
356721
353996
350322
346870
344487
344115
343291
339386
339434
335792
332567
327624
322527
320814
320194
318670
314587
311230
309643
308477
305774
305622
304997
304688
302726
302340
306885
307094
306655
303944
302795
305858
305333
304676
308960
308730
309661
305969
303449
303453
305807
308314
302173
301391
309640
308978
319871
326190
322059
313049
314469
320909
320208
327305
326117
326098
324705
318250
320023
314409
312206
311471
297712
294166
302082
305917
302395
304460
299930
296731
294601
290178
285573
283062
And the calculated variance in excel using VAR.P function is "61222801119"
This is one of the functions that I have tried
function getVariance1($arr){
$arr_size=count($arr);
$mu=array_sum($arr)/$arr_size;
$ans=0;
foreach($arr as $elem){
$ans+=pow(($elem-$mu),2);
}
return sqrt($ans/$arr_size);
}
which returns "1633416980.1583" that is way wrong.
What's the problem ?
Thanks
I'm trying to get percentage of two numbers in my app (laravel based) but i don't get the right numbers.
Code:
$pdts->price = 123.234
$pdts->newprice = 90.500
{{number_format($pdts->newprice / $pdts->price * 100, 0) } }%
it returns -73%, it should return -27%. How do I correct this?
Update To those still didn't get it
Guys I did defined my goal / what i'm looking for it should return -27%. How do I correct this? so I was looking to get -27% as the result and thanks to commenters I did find my solution.
I don't understand all this vote-downs and continued comments.
This is not really a programming question, it's more a math question.
But to get the result you want you need to calculate the difference in percentage.
//(123.234-90.500)/123.234*100
- {{number_format(($pdts->price - $pdts->newprice) / $pdts->price * 100, 0) } }%
// -27%
Try this one,
$value = ($pdts->price-$pdts->newprice) / $pdts->price * 100;
$percentage = (int)($value).'%';
Here, percentage variable will have your required value.
I am trying to find a php equivalent of processing's "map" function so I can re-map a number from one range to another. Does anything exist? Is it called something different?
http://processing.org/reference/map_.html
For example to map a value from 0-100 to 0-9.
map(75, 0, 100, 0, 9);
There is no native function for doing this, but it's easy to create:
function map($value, $low1, $high1, $low2, $high2) {
return ($value / ($high1 - $low1)) * ($high2 - $low2) + $low2;
}
This is untested, but you should hopefully get the idea.
Thanks for this great function!
I would personally improve a bit this function by adding simple error checking, to avoid dividing by 0, which would make PHP have a fatal error.
function map($value, $low1, $high1, $low2, $high2) {
if ($low1 == $high1)
return $low1;
return ($value / ($high1 - $low1)) * ($high2 - $low2) + $low2;
}
Im going through a log file using php and it looks either like this:
11/06/05 09:17:59 TORMS068 11/06/05 09:17:59.234 TORMS068\Admin ... EPTH{2} ITEMIX{8} TELL{`` sdcsit49 - FileSystem /oracle/REF/sapdata2 Critical - MSGREC:1727:100 ``} USE{TELL} ATTACHMENT... xact{`NO`}
Where I used Ellipses to show there were a lot of other stuff
OR like this
11/06/05 11:29:38 TORM ... H{3} ITEMIX{5} TELL{``marble: initiator SCSI ID now 7 } File={ /var/adm/messages } - MsgRec 5174:406``} USE{TELL} ATTACHMENT{} UserParms{} AnswerWait{`10`} BaudRate{`1200`} C... eviceId{``} TellExact{`NO`}
So it is either followed by a USE{TELL} or File={.*}
I want to extract what is in the {}'s for TELL{} for every line in the log file..
Please help me, I'm going crazy lol
Thanks
if (preg_match("/USE\{(?<insideTheBrackets>[^\}]+)\}/", $line, $pat)) {
var_dump($pat['insideTheBrackets']);
}
PHP's regular expression documentation is pretty good at explaining this stuff...
<?php
$str = "11/06/05 11:29:38 TORM ... H{3} ITEMIX{5} TELL{``marble: initiator SCSI ID now 7 } File={ /var/adm/messages } - MsgRec 5174:406``} USE{TELL} ATTACHMENT{} UserParms{} AnswerWait{`10`} BaudRate{`1200`} C... eviceId{``} TellExact{`NO`}";
$pat = '/TELL{(``.*``)}/';
preg_match($pat,$str,$matches);
print_r($matches);
?>
Well I couldn't get a reliable method to work, but what I ended up with was
if (preg_match("/(TELL{)(.*)} (USE{T)/iU ",$data,$matches))
$rowMsg=substr($matches[0],5,-7);
$rowMsg=preg_replace("/[}\. ]{1}\$/",'',$rowMsg);
I m having a application in which i have to select a number out of many numbers according to their weights. Every time I will select , I have send the result to flash.I have found a algorithm in python. I have implemented it in php and was testing for its results. If i was running that algo in python it was giving good results but in php not so good. Ex. (1=>30,2=>40,3=>30) After running many times , the probablity of occurence first number in weighted array is always more but in python it is uniform. I have attatched the PHP code.
define("MAX",100000);
$reelfrequencies=array(30,40,30);
echo weightedselect($reelfrequencies);
/*function weightedselect($frequency)
{
$arr=cumWghtArray($frequency);//array(35,96,100);
print_r($arr);
$len=sizeof($frequency);
$count=array();
echo $r=mt_rand(0,$arr[$len-1]);
$index=binarysearch($arr,$r,0,$len-1);
return $index;
}*/
function cumWghtArray($arr)
{
$cumArr=array();
$cum=0;
$size=sizeof($arr);
for($i=0;$i<$size;$i++)
{
$cum+=$arr[$i];
array_push($cumArr,$cum);
}
return $cumArr;
}
function weightedselect($frequency)
{
$arr=cumWghtArray($frequency);//array(35,96,100);
$len=sizeof($frequency);
$count=array();
$count[0]=$count[1]=$count[2]=0;
for($i=0;$i<MAX;$i++)
{
$r=mt_rand(0,$arr[$len-1]);
$index=binarysearch($arr,$r,0,$len-1);
$count[$index]++;
}
for($i=0;$i<3;$i++)
{
$count[$i]/=MAX;
echo $i." ".$count[$i]."\n";
}
}
function binarySearch($ar,$value,$first,$last)
{
if($last<$first)
return -1;
$mid=intVal(($first+$last)/2);
$a=$ar[$mid];
if($a===$value)
return $mid;
if($a>$value&&(($mid-1>=0&&$ar[$mid-1]<$value)||$mid==0))
return $mid;
else if($a>$value)
$last=$mid-1;
else if($a<$value)
$first=$mid+1;
return binarySearch($ar,$value,$first,$last);
}
Here is the Python Code. I have taken this code from this forum .
import random
import bisect
import collections
def cdf(weights):
total=sum(weights)
result=[]
cumsum=0
for w in weights:
cumsum+=w
result.append(cumsum/total)
return result
def choice(population,weights):
assert len(population) == len(weights)
cdf_vals=cdf(weights)
x=random.random()
idx=bisect.bisect(cdf_vals,x)
return population[idx]
weights=[0.30,0.40,0.30]
population="ABC"
counts={"A":0.0,"B":0.0,"C":0.0}
max=10000
for i in range(max):
c=choice(population,weights)
counts[c]=counts[c]+1
print(counts)
for k, v in counts.iteritems():
counts[k]=v/max
print(counts)
Problem is of mt_rand() function which is not uniform. The python random.rand() is very much uniform. Which random function should i implement in php with a proper seeding value every time it runs. I was thinking of using Withcmann (used by python random.random) but how will i provide the seed.
Both rand and mt_rand should both be more than sufficiently random for your task here. If you needed to seed mt_rand you could use mt_srand, but there's no need since PHP 4.2 as this is done for you.
I suspect the issue is with your code, which seems unnecessarily involved given what I believe you're trying to do, which is just pick a random number with weighted probabilities.
This may help: Generating random results by weight in PHP?