• Status: Solved
• Priority: Medium
• Security: Public
• Views: 470

# Help converting math function into pseudo code

I found an interesting article (to me at least) that I want to play around
with, but am not fluent in Greek (Pure mathematical functions) .

http://www.bouncingchairs.net/pskalman-lategecco.pdf
the algorithm itself starts around page 4 (5 is graphs), 6 has its 'pseudo code',
which is all Math.

I don't understand what
norm
Normal
||v_t|| or ||vt_best|| (the double pipes, or how to evaluate them)

in this context mean.
Any nudge in the right direction is super appreciated.

THanks,
0
JustinW
• 13
• 10
• 2
1 Solution

Commented:
I guess in this paper by ||u|| they mean the length (absolute value )
of the vector
0

Commented:

like u/||u||
Normalization of the vector means making the vector in the same direction
but with unit length and to get it you need to divide each
coorduinate by the length of the vector
0

Commented:
So if vector has say three components

u(x,y,z)

then ||u|| = SQRT(x*x + y*y + z*z)
0

Author Commented:
It looks like in wikipedia (http://en.wikipedia.org/wiki/List_of_mathematical_symbols)

|| x || means the norm of the element x of a normed vector space

So, equation 7 would go like this?

``````for (i = 0; i < n; i++)
{
v[i] += v[i]/SQRT(SUM(v[i]^2..v[n]^2))
+ (1-0.45)
* vb[i]/SQRT(SUM(vb[i]^2..vb[n]^2))
}
``````
0

Commented:
In equation (5), the article has the expression: alpha * u1 / || u1 || where u1 is a vector. I happen to know that u1 / || u1 || is a way to create a unit vector from the vector u1. (The unit vector is a vector in the same direction as the original, but having a length of 1.)

To do this scaling, you have to divide the vector by the vector length.

If the vector is in a 3D vector space, for example, then it has three coordinates.

u1 = xi + yj + zk = (x, y, z)

Then || u1 || = sqrt( x² + y² + z² ) = the length of u1.
0

Commented:
I should have refreshed this open page to see what else had been written.
0

Commented:
Yes, you could call it the norm, it is easier to think of ||u|| value as a  length
of the vector, especially when you think in 3D space

u/||u|| will indeed be called normalized vector - this is the vector
which has the same direction as original vector u, but is of a unit length

So you obrtain such vector by dividing all components
of original vector by the length of the vector
0

Author Commented:
if my interpretation of equation 8 is correct,
what do you do with equation 9?

``````v(hat)[i] ~ Normal(0.6, v[i])
``````
0

Author Commented:
**Correction to the first bit of code, that shouldn't be +=; just =
0

Commented:
Equation (9):

float [] vt = new float[3];

float [] vbest = new float[3];

float []  v = new float[3];

float [] vtplus1 = new float[3];

float alpha;

flaot lengthvt = Math.sqrt(vt[1]*vt[1] + vt[2]*vt[2] + vt[3]*vt[3]);

flaot lengthvbest = Math.sqrt(vbest[1]*vbest[1] + vbest[2]*vbest[2] + vbest[3]*vbest[3]);

float lengthv = Math.sqrt(v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);

for (int j=0; j<3; j++) {

vtplus1[j] =( (alpha*lengthvt + (1.0f-alpha)lengthvbest)/lenghtv)*v[j];
}

vtplus1[j] = (alpha*
}

0

Commented:

Equation (9) (correction):

``````float [] vt = new float[3];

float [] vbest = new float[3];

float []  v = new float[3];

float [] vtplus1 = new float[3];

float alpha;

flaot lengthvt = Math.sqrt(vt[1]*vt[1] + vt[2]*vt[2] + vt[3]*vt[3]);

flaot lengthvbest = Math.sqrt(vbest[1]*vbest[1] + vbest[2]*vbest[2] + vbest[3]*vbest[3]);

float lengthv = Math.sqrt(v[1]*v[1] + v[2]*v[2] + v[3]*v[3]);

for (int j=0; j<3; j++) {

vtplus1[j] =( (alpha*lengthvt + (1.0f-alpha)lengthvbest)/lenghtv)*v[j];
}
``````
0

Commented:
In equation (7) you also need multiply by 0.55f* before the first part of the sum
assuming that 1-alpha = 0.45
0

Author Commented:
I'm sorry, I got my numbers jumbled;
what is this?
``````Equation 8; it says v[i] ~ norm(sigma^2, v[i])
``````
0

Commented:
I guess this is not so simple - what they mean
that you should generate random v by generating random numcbers
for each coordinates, but this random numbers
should be distributed normally around value v' as math expectation (for each coorduiinate)
with dispersion of sigma - i guess sigma was also acalculated before?

For this you probably need to write a special program - you can
0 and 1 but then calculate value according the norma distribution
- I think we should browse around there should be such normally distributed
random generator in java somehwere, so you see you need to get vlaues close to v'
much more often than far away from v' (as opposed to uniform random which we have in
our standard java lbrary)

0

Commented:
0

Author Commented:
very nice indeed!

so v[j] = random number normally distributed around v[j] with a stdev of 0.6?
or something else entirely?
0

Commented:

yes, that's what it is I guess
I'm not yet sure how to get form their distribution with average 0 and dispersion 1
to your requiirement but I'm sure t is doable and should not be too hard
average is just to move - dispersion is probably just to delete by square root or somthing
0

Author Commented:
This should keep me busy for a while.
I'm sure I'll be back for more when I get stumped,
but in the meantime; thanks!
0

Author Commented:
Also,

(2*rand-1 + 2*rand-1 + 2*rand-1)*stdev + avg
appears to generate a random number normally distributed
around an average. Might be quicker too
0

Commented:
Yes, that seems logical  - I guess by  2*rand - do you mean 2 in power of rand ?
0

Author Commented:
I have no idea why it works (or if it really does), but
its just straight multiply

I tested it in excel, and the AVERAGE function gave me the
average I put in; and the STDEV function roughly gave me the
stdev I put in

I checked this against the results from using = norminv(rand(),avg, stdev),
and they appear indistinguishable.

so I'd use (-1 + 2 * rand) + (-1 + 2 * rand) + (-1 + 2 * rand)
0

Author Commented:
all that, * stdev + average
0

Commented:

Well, if you have the same average and the same std dev,
it does not necessarily mean that the distribution
would be Gaussian

On the other hand we don't know if it is essential that it would be Gaussian
or just average and dispersion is important
0

Commented:
OK, so that turns out to be very simple - you take
this java standard methhod

nextGaussian() - it will return you the numbers distributed normally
with avergae 0.0 and stnd deviation 1.0

So if you multiply this number by standard deviation you need
and add the avergae you want then the new number will be distributed exavtly as you need so your method
for generating such values wuold be:

Random r = new Random(new java.util.Date().getTime();

double myGaussian(double avg, double std_dev, Random r){

double x = r.nextGaussian();

return (x*std_dev + avg);

}

So if you call

myGaussian(5, 25, r);

million times than your resulst will be ditributed over Gaussian centered at 5 with standard deviation 25

0

Author Commented:
I felt like modeling this in ruby to work out some of the kinks
before shifting platforms.

Here's what I have so far to describe a particle (hawk)

``````class Hawk
attr_accessor	:pos,
:vel,
:vprime,
:vhat,
:denom

#---------------------------------------------------------
def update!(best_p)

_denom!
_vprime!(best_p)
_vhat!

@vel.each_index do |x|
@vel[x] = (@alpha * @denom + (1 - @alpha) * best_p.denom) * @vhat[x]
end
end
#_________________________________________________________

##########################################################
private
##########################################################

#---------------------------------------------------------
def _denom!
b = 0
@vel.each{|x| b += x**2}
@denom = b**(1/2)
end
#_________________________________________________________

#---------------------------------------------------------
def _vprime!(bp)
@vel.each_index do |x|
@vprime[x] = @alpha *
(@vel[x] / @denom) +
(1 - @alpha) *
(bp.vel[x] / bp.denom)
end
end
#_________________________________________________________

#---------------------------------------------------------
def _vhat!
@vprime.each_index do |x|
@vhat[x] = @vprime[x].normal(@sigma)
end
end
#_________________________________________________________

end
``````
0

## Featured Post

• 13
• 10
• 2
Tackle projects and never again get stuck behind a technical roadblock.