Finding three functions that interpolates between equitation - function

For a past week I been trying to construct specific functions that would satisfy some the requirements, sadly it was not successful. I decided to give a shot in stack overflow.
I have three shapes in 2D world as shown in the image. Shape A with center a, Shape B with center b and shape C with center c. For this problem shape form does not matter so I will not expand on that. Other important variables are d_ab with represents minimum distance between shape A and B, d_bc - distance between B and C, d_ac - distance between A and C. Also d_ab, d_bc, d_ac ranges from 0 to 1, where 1 represents shapes are touching and 0 their too far from specific threshold.
Here is the image of it
I am trying to find three new functions:
f_a(d_ab, d_bc, d_ac) = x
f_b(d_ab, d_bc, d_ac) = y
f_c(d_ab, d_bc, d_ac) = z
Firstly, these functions with inputs d_ab, d_bc, d_ac in the left side table should result to values x, y, z right side table:
d_ab
d_bc
d_ac
x
y
z
1
0
0
(a+b)/2
(a+b)/2
c
0
1
0
a
(b+c)/2
(b+c)/2
0
0
1
(a+c)/2
b
(a+c)/2
1
1
1
(a+b+c)/3
(a+b+c)/3
(a+b+c)/3
0
0
0
a
b
c
Secondly, all other inputs d_ab, d_bc, d_ac that are not described in table (From 0 to 1) should result in interpolated values between closest ensuring continuity (Continuity I am referring there is that small change in input should not produce big difference in output, there is probably better way to describe this mathematically).
For example:
f_a(0.3, 0, 0) = x
Most likely will combine equitation's from table:
f_a(0, 0, 0) = a
f_a(1, 0, 0) = (a+b)/2
Resulting into:
f_a(0.3, 0, 0) = 0.7 * a + 0.3 * (a+b)/2
I am curious is there a method to find these kind of functions, I assume there could be more than one that satisfies this requirement. But I am fine with any of it as long it has that continuity that I referred above.
I almost sure there is better way to represent this problem mathematically, but I am not that great with it.

Related

SQL generate a random positive or negative value

I am looking for a way to change a value randomly from positive to negative. (I am creating a distortion on a lat/long location, so I would like to offset a given location with +/- some degrees)
I already created the following query which give me a number between -1 and +1, the idea is to multiply my distortion with this number to get a random negative or positive number.
SELECT round(-1+3*RAND(),0);
The only problem is, this also generates the value 0.0 which can't be multiplied. How do I get -1 or +1 only?
TIA
ABBOV
Maybe:
start by rounding, to give 0 or 1
then multiply to give 0 or 2
then subtract, to give -1 or 1
i.e.:
SELECT ROUND(RAND()) * 2 - 1;

MySQL GROUP BY if Multiple Numbered Columns are Close to Each Other (+/- 1)

I have a mysql table with a large list of coordinates (x, y, z). I want to find the most common spots, but when the same place is logged, it isn't identical. For example, x could be 496.0481 or 496.3904, but that is actually the same place.
When I do the following query I get a list of the absolute exact matches, but those are very few and far between:
SELECT x, y, z, COUNT(*) AS coords
FROM coordinates
GROUP BY x, y, z
ORDER BY coords DESC
LIMIT 10;
How can I adjust this to be grouped by each of x, y, and z to be +/- 1 to catch a larger area? I've tried a mix of IF and BETWEEN statements but can't seem to get anything to work.
If I do GROUP BY round(x), round(y), round(z), that gets a larger range but doesn't capture if the number goes from 496 to 497 even if they are just slightly different.
Thanks in advance for the help.
Very naive way:
select t1.x as x1, t1.y as y1, t1.z as z1, t2.x as x2, t2.y as y2, t2.z as z2
from coordinates t1
join coordinates t2 on sqrt(power(t2.x-t1.x, 2) + power(t2.y-t1.y, 2) + power(t2.z-t1.z, 2)) <= 1
For each coordinates (t1) query finds all other coordinates (t2) that dinstanced less or equal than 1 from each other.
But this query has very bad performance: O(n^2)

AX=B,calculate complex matrix,while A and B is many dimensions

now I calculate AX=B ,while A X B is complex matrix,if i set size A is 3*3,B is 3*1,and it Calculate the correct.But if i set A is 58*58 and B is 58*256 complex matrix,it Calculation error.And i see the error happen at cublasCgemv,.I see the L have value,and d_B have value also,after cublasCgemv,there is no value ,

Picking random row with certain chance (weight) in MySQL

I searched this for a while but results are just confusing my head because I am quite new on MySQL.
I have a table with these 4 columns: AUTO_INCREMENT ID, NAME, TYPE, CHANCE so rows look like this:
1, NOTHING, NO, 35
1, VERSICOLOR, TREE, 35
3, DIVERSIPES, TREE, 35
4, AMAZONICA, TREE, 35
5, EMILIA, GROUND, 25
6, BOEHMI, GROUND, 25
7, SMITHI, GROUND, 25
8, METALLICA, SKY, 5
9, REGALIS, SKY, 5
Note: Those are simple examples, there will be x100 like them. What I need to do is picking one row from this table with chances as shown in CHANCE column.
Meaning; I need to pick one row from 9 of them and results can be "VERSICOLOR, DIVERSIPES, AMAZONICA or NOTHING with 35% chance" or "EMILIA, BOEHMI or SMITHI with 25% chance" or "METALLICA or REGALIS with %5 chance". So this query will probably give me the result of "VERSICOLOR, DIVERSIPES, AMAZONICA or NOTHING" because it has 35% chance or maybe I am gonna be lucky and I will get the "METALLICA or REGALIS" :)
Basicly there are 3 group of types, GROUND, TREE and SKY. What I want to do is getting only one result from all of these. GROUND, TREE or SKY type item with certain chances but to be certain, I dont want one for each group, I want only one result, it can be item of GROUND, TREE or SKY type.
I hope that I did explain myself. Regards.
There is probably a more elegant solution and one that doesn't assume your percentages add to 100 - but this may work:
Example http://sqlfiddle.com/#!2/ec699/1
SELECT *
FROM (
SELECT id, name, type, chance
, #value + 1 AS lowval
, #value := #value + chance AS hival
FROM tbl
JOIN (SELECT #value := 0) AS foo) AS bar
JOIN (SELECT FLOOR(1 + RAND()*99) AS guesser) AS bar2
ON guesser BETWEEN lowval and hival;
This problem comes up outside SQL. I posted a general solution here, generate random numbers within a range with different probabilities.
With the first set of numbers you used (70%, 25%, 5%) fill three urns with 100 balls each. Urn 0 all balls are red. Urn 1 75 of the balls are green, 25 balls are red. Finally, urn 2 will have 15 blue balls and 85 red balls. Now pick a random urn, each having probability 1/3, and pick a random ball. Using this scheme the probability of getting a red ball is 0.75, the probability of a green ball is 0.25 and the probability of a blue ball is 0.05.

A simple vote aggregation function

I want to evaluate the acceptance rate of a proposal, where a proposal can receive two types of votes: namely positive and negative.
So the simplest function that comes to mind is as follows:
p + n / p + n + \epsilon
However I would like to come up with a more sophisticated function which would satisfy the following two properties.
The ratio of positive votes to the total amount of votes should always take precedence. So where p1 = 5, n1 = 0, p2 = 99, n2 = 1 the function should calculate a higher acceptance rate for the first one.
When the ratios are equal, the function should return a higher acceptance rate for the one with the higher number of total votes. So in the following case where p1 = 1000, n1 = 0, p2 = 10, n2 = 0 again the first one should have a higher acceptance rate.
Another idea concerning the function could be the following:
w * [p / (p + n)] + (1 - w) * [(p + n) / maxV]
where maxV is the maximum number of votes that any proposal received and w is a real number in [0..1].
This function satisfy the second condition whereas the guarantee does not extend to the first one. Finding a value for w to satisfy the system could be cumbersome thus I'm searching for a better solution.