I'm writing a very simple function where I am supposed to find the distance between two 3 d points. One set of points are given directly as floats, whereas the others are given as a horizontal array.
The three inputs are x,y,z which are the floats and a row pickups[0] being passed as par which is ["blue1",441.2223, 231.44, 127.7466]
Now, the row comes up as an object of course. I was having a problem with making a function to calculate the distance: My code as of now is:\
function distance(x,y,z,par)
{
var p:float;
p=Mathf.Sqrt( (x-parseFloat(par[1].ToString())) * (x-parseFloat(par[1].ToString())) + (y-parseFloat(par[2].ToString())) * (y-parseFloat(par[2].ToString())) + (z-parseFloat(par[3].ToString())) * (z-parseFloat(par[3].ToString())));
return p;
}
Please try and help me out.
The error has to do with var p:float;, you don't need to try and predeclare the variable p as anything as javascript is type-less. What the reference is refering to with static function Sqrt (f : float) : float is that it is expecting a variable that is a float and returns a float. Your code should be written as:
function distance(x,y,z,par)
{
var p = Mathf.Sqrt( (x-parseFloat(par[1])) * (x-parseFloat(par[1])) + (y-parseFloat(par[2])) * (y-parseFloat(par[2])) + (z-parseFloat(par[3])) * (z-parseFloat(par[3])));
return p;
}
You also don't need toString() your parameters as they will be parsed into a float.
Related
Disclaimer: I have almost no mathematics notions, so this question could be very basic for some of you.
I'm searching for the name of a concept which consists in combining pure functions together (say, functions with the same input and output types and number of parameters) to make them simpler.
Suppose I have these 3 methods with the same signature:
addOne(param: number): number {
return param + 1;
}
addTwo(param: number): number {
return param + 2;
}
multiplyByThree(param: number): number {
return param * 3;
}
Now I know that I'll always use these functions in the same order and same param.
Ex: I will process a sound or an image.
I want to avoid uselessly applying coefficient or offsets that could be computed together (optimization/regression).
Let's say I have this imaginary library with a method called computeOptimizedFunction that applies this concept to my functions. It takes any number of functions with the same signature as input.
var optimized = computeOptimizedFunction(addOne, addTwo, multiplyByThree);
Actually equals to:
var optimized = (param: number) => 3 * (param + 3);
Anyone here has an idea of how this concept or pattern is called, if it exists?
I'm working on a simple gravity program in Processing. My program takes particles and attracts them to each other based on the formula for gravity. Unfortunately, once I try to multiply the force to the direction with PVector.mult(), I get the error in the title:
Cannot invoke mult(float) on the primitive type float.
Here is my code for the method. G is defined elsewhere.
public float distance(Particle other) {
return location.sub(other.location).mag();
}
public PVector direction(Particle other) {
return location.sub(other.location).normalize();
}
public void gravity(Particle other) {
float grav = (G*((mass * other.mass)/pow(distance(other), 2)));
if(distance(other) != 0) {
acceleration.add(distance(other).mult(grav));
}
Why am I not able to pass a float where a float is due?
Let's take this line apart and split it into multiple steps:
acceleration.add(distance(other).mult(grav));
Here's my attempt to split it into multiple lines:
float grav = 42;
float distanceFromOther = distance(other);
float multipliedValue = distanceFromOther.mult(grav);
acceleration.add(multipliedValue);
Hopefully this makes it more obvious what's going on: you're trying to call mult() on a float value, which won't work. You need to call mult on a PVector or another class that contains a mult() function.
I did not understand how the function call pic.Show(Pic) works and what it does.
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
pic := make([][]uint8, dy)
for y := range pic {
pic[y] = make([]uint8, dx)
for x := range pic[y] {
pic[y][x] = uint8(5 * (x + y))
}
}
return pic
}
func main() {
//pic.Show(Pic(40,30)) // doesn't work, but why?
pic.Show(Pic) // works, but why? Where are the values for dx and dy set?
}
There is, starting at line 5, a function, named Pic, and it receives two integer variables (dx, dy). So I think, a correct function call might be Pic(40,30) (with 40 and 30 being the values for dx and dy).
But why does line 17 throw an error? (pic.Show(Pic(40,30)))
Why does line line 18 work? (pic.Show(Pic))
And where do the values of dx and dy come from when line 18 is executed?
I tried to look up http://golang.org/x/tour/pic which redirects me to https://godoc.org/golang.org/x/tour/pic. There I can read, that the function Show is defined this way:
func Show(f func(int, int) [][]uint8)
Which I understand as:
Show is a function that needs one parameter. This parameter is a function that needs two parameters, both of type int, and it has to return a value of type [][]uint8 (a slice of slices of unsigned 8-bit integers). Show itself doesn't return anything.
So, here again, I read that the inner function (the parameter of Show) needs two parameters. But why do I get an error, when I try to provide those parameters? Why is it ok to call the function Pic without parameters? And where do the values for those parameters come from, when Pic is executed?
When you say Pic(40, 30) you call the function Pic and it returns a [][]uint8 (as seen in the function definition). This means that in your commented-out code you pass a [][]uint8 to Show.
When you say Show(Pic) you pass Pic as the parameter, which is a function. That is what Show expects. Pic is of type func(dx, dy int) [][]uint8.
Go allows you to pass functions around as parameters and that is what is happening here.
You are quite right about definition of Show - it is a function which accepts another specific format notion as a parameter.
Pic is such a function matching this criteria - so you pass it to Show successfully.
But when you call Pic(30,40) that means not a function but a result of calling the function with such parameters. So in this case you passs to Show not a function Pic but a slice returned by it [][]uint8. Of course Show can’t accept it.
Your function, Pic, takes two parameters, Show takes one. You're calling Show with a single parameter, which is a function; that function takes two parameters. When you pass a function to another function, the assumption is that the function you're calling (Show) will call the function you passed in (Pic) and provide the necessary parameters when it makes that call.
I have created a function that returns the magnitude of a vector.the output is 360x3 dimension matrix. the input is 360x2.
Everything works fine outside the function. how do i get it to work ?
clc
P_dot_ij_om_13= rand(360,2); // 360x2 values of omega in vectors i and j
//P_dot_ij_om_13(:,3)=0;
function [A]=mag_x(A)
//b="P_dot_ijOmag_"+ string(k);
//execstr(b+'=[]'); // declare indexed matrix P_dot_ijOmag_k
//disp(b)
for i=1:1:360
//funcprot(0);
A(i,3)=(A(i,2)^2+A(i,1)^2)^0.5; //calculates magnitude of i and j and adds 3rd column
disp(A(i,3),"vector magnitude")
end
funcprot(1);
return [A] // should return P_dot_ijOmag_k in the variable browser [360x3 dim]
endfunction
mag_x(P_dot_ij_om_13);
//i=1;
//P_dot_ij_om_13(i,3)= (P_dot_ij_om_13(i,2)^2+P_dot_ij_om_13(i,1)^2)^0.5;// example
You never assigned mag_x(P_dot_ij_om_13) to any variable, so the output of this function disappears into nowhere. The variable A is local to this function, it does not exist outside of it.
To have the result of calculation available, assign it to some variable:
res = mag_x(P_dot_ij_om_13)
or A = mag_x(P_dot_ij_om_13) if you want to use the same name outside of the function as was used inside of it.
By the way, the Scilab documentation discourages the use of return, as it leads to confusion. The Scilab / Matlab function syntax is different from the languages in which return specifies the output of a function:
function y = sq(x)
y = x^2
endfunction
disp(sq(3)) // displays 9
No need for return here.
Anyone had an issue with bogus math results when using Google Apps Script? The following is the code, where a/b/c are send from another function. A/b/c are verified with the logger. ? StartPt is fine, but endpt is generating a false value.
function calcTime(a,b,c) {
var x;
var startHr = 8;
var startMin = 0;
var startSec=0;
var startPt;
var endPt;
startPt = (startHr * 60) + startMin + (startSec/60);
endPt = (a * 60) + b + (c/60);
Logger.log(endPt);
x = endPt -startPt;
return x;
}
flag
I found that the math error is a multiplication factor of 100 (when endPt is calculated, it multiplies the result by 100). What could cause this?
Real number arithmetic is prone to rounding errors in javascript and apps-script. See Is floating point math broken?. If you're watching your function in the debugger, you'll see rounding errors esp. with (c/60). But that's not likely causing a factor-of-100 error.
Most likely, your parameters aren't what you thing they are. If b arrives as a string, for instance, the calculation of (a * 60) + b + (c/60) will effectively ignore b. The other two parameters, however, will get changed to numbers to to complete the multiplication and division operations. (You can avoid that by using b * 1.)
Anyway, to confirm what you're getting as parameters, try replacing the first few lines of your function with this:
function calcTime(a,b,c) {
var params = {
a:{type:typeof a, value:a},
b:{type:typeof b, value:b},
c:{type:typeof c, value:c}}
Logger.log(params);
var x;
...
Verify that you're getting what you need. If any of the parameters are arriving as date objects, for instance, your math will be wildly incorrect. You may just need to enforce types for your data source.