Linear interpolation library with php [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I need to interpolate points with php, do you know any library for that? A traditional search did not allow me to find any that good results.
I have a table with values in columns x and y.
X Y
10 676
20 894
30 1100
40 1200
50 1000
I was wondering if there is a function that can help with linear interpolation. For example, I want the interpolated value of Y that corresponds to X=35.
Thanks

That's simple math - no need to use any library at all.
If you want the Y value of X, you have to find the greatest value smaller than X (x0) and the lowest value greater than X (x1).
If this two values are equal, you don't have to do anything and just return the Y value in your table.
Otherwise, take the two corresponding Y values (y0 and y1) and do a interpolation with them.
d = (x - x0) / (x1 - x0) // value in the range of [0; 1]
y = y0 * (1 - d) + y1 * d // your interpolated value
in your case of x = 35 => x0 = 30, x1 = 40
d = (35 - 30) / (40 - 30) // 0.5
y = 1100 * (1 - 0.5) + 1200 * 0.5 // 1150

Related

How to speed up these R computations? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have to data frames with X, Y, and Z coordinates. I want to find the distance between all of the points in the two data frames. (Like the distance between entry A1 and every entry in B, A2 and every entry in B, and so on, and vice versa). I basically did this:
1.) Wrote a function that calculates the distance between two points.
2.) Used the distanceFinder function to create a function that finds the distance between one point in a group, and every other point in the opposite group.
3.) Created a function called bigDistance() that calls filter() on every entry in one group, and appends the results to an empty data frame through a for loop until it's completed.
This code takes about 2 minutes to run on the file I'm experimenting with, and I just found out that I have to translate this algorithm to PHP... so I guess this is kind of an optimization question, because I feel like PHP would be way slower at making these computations than R? Sorry if people find this "off-topic" but yeah, super new to programming and Big O notation and stuff, so any tips would be amazing! Thanks!
The dist function does exactly what you are looking for.
myDf <- data.frame(
x = rnorm(8),
y = rnorm(8),
z = rnorm(8)
)
dist(myDf)
# 1 2 3 4 5 6 7
# 2 3.0457054
# 3 1.7260658 3.2107845
# 4 1.2839101 3.4596211 2.9451175
# 5 1.5656231 4.0154389 2.3421445 2.3612348
# 6 1.9294650 1.6655718 1.7977887 2.8726174 2.5815296
# 7 2.1842743 3.5274692 3.8552701 1.0984651 2.9951244 3.3220919
# 8 1.4795857 3.5364663 0.5567753 2.7033371 1.9226225 2.0631788 3.6624082
It seems to be pretty fast as well (73ms on average)
library(microbenchmark)
mb <- microbenchmark(dist(myDf))
mb
# Unit: microseconds
# expr min lq mean median uq max neval
# dist(myDf) 70.436 71.453 77.4083 72.978 82.133 172.911 100
autoplot(mb)

How to get out number - php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to find out how I can extract $number out of the following code:
$calc = ($number % 10000) / 100;
So what's the opposite of %?
%, or the modulo operator, means "remainder after division by".
For example: 18 % 4 = 2, because if you divide 18 by 4 you get 4 with a remainder of 2. In the other direction, we can see that indeed 4 * 4 + 2 = 18.
Now consider the following:
18 % 4 = 2
22 % 4 = 2
26 % 4 = 2
...
2445678 % 4 = 2
As you can see, there are multiple values that are what we call congruent modulo 4. Therefore an inverse function of modulo cannot exist, because there is an infinite amount of possibilities.
Several cryptographic functions are based on the fact that the above holds true.
Generally you can't. % gives you the modulo of a division, there are mathematically an infinity of numbers that have the same modulo for a given ratio.
It is simply not bijective..

questions related to source code of PHP uniqid() [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
In PHP source code function uniqid() have following C code:
(I removed some types to shorten it)
//...
struct timeval tv;
gettimeofday(&tv, NULL);
int sec = (int) tv.tv_sec;
int usec = (int) (tv.tv_usec % 0x100000);
// The max value usec can have is 0xF423F,
// so we use only five hex digits for usecs.
printf("%08x%05x", sec, usec);
//...
If we put criticism aside, they try to produce 64 bit timestamp.
0xF423F is probably CLOCKS_PER_SEC - 1 (CLOCKS_PER_SEC is decimal 1000000),
but where this 0x100000 come from and what could be the reason to use modulus instead of bitwise and?
She or he could write the Unique ID as printf("%08x%08x", sec, usec)
sample output:
55189926000eb16f
5518997900051219
5518997a0005171b
The zeros in position 8 to 10 are consistent, they don't add entropy, so he wants to get rid of those zeros. The new UID will be 3 bytes shorter with the same entropy. He could simply use printf("%08x%05x", sec, usec);
sample output:
55189926eb16f
5518997951219
5518997a5171b
But that's on the assumption that usec is guaranteed to be less than 0x100000 otherwise UID will be up to 16 bytes long. You need % 0x100000 for insurance. It's also the same as & 0xFFFFF. Technically the insurance should be % 1000000 (decimal), but it doesn't really matter, it's still the same entropy.
Or we could just use the 16 byte version because saving 3 lousy bytes don't matter these days.

How do make this calculation to achieve the answer I want? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is a bit of a puzzle. Look at the sum and result below:
$markTotal += ($session['Mark'] / 100 * $session['SessionWeight']);
Result on Browser:
Module: CHI2550 - Modern Database Applications 41.2 (this is $markTotal)
Session: AAB 72(this is $session['Mark']) 20% (this is $session['SessionWeight'])
Session: AAE 67(this is $session['Mark']) 40% (this is $session['SessionWeight'])
As you can see the answer to the calculation above is right, the answer is 41 .2 as it adds up the two session marks, divide by 100 and then times it by the total amount of the percentage.
But I want to include a total mark where except it is out of the total session percentage (60% for above example), it is out of a 100% but I can not work as simple as that as in above example one session is worth more than other. I have worked out that the answer for the total mark of the above example out of 100% should be 69, but how do I achieve this in my calculation.
Thank you and any help is much appreciated :)
You'd need to add up the total marks as well. So if session AAB has 90 total marks available (and the student got 72) and AAE has 80 marks (and got 67) then it'd be
(72) + (67) 14.4 26.8 41.2
(--) * 0.2 + (--) * 0.4 = ---- + ---- = ---- = 79.23%
(90) + (80) 18 34 52

Maths behind sliding scale pricing [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm trying to work out a simple calculation for the following:
a phone model has the maximum sale price of £85.00 and this is if only 1 unit is purchased and a minimum sales price of £50.00 - this is if 150 units and over are purchased in one.
How can I work out a way of the price if between 2 and 149 units are purchased??
Thanks,
B.
Formula:
Y = 50 + ((85 - 50) / (150 - 1)) * (X - 1)
Result:
X = 1 --> Y = 85
X = 33 --> Y = 57.52
X = 150 --> Y = 50
You probably don't have a linear model in such situations. I don't know the right English term, but you would have more like a piecewise or stepwise function here.
But nevertheless, you can get a linear function through interpolation.

Categories