How can we convert ParArray[(Double, Double, Double, Double, Double)] to Array[(Double, Double, Double, Double, Double)]
I have to do this as part of creating the data frame using sc.parallelize(Array[(Double,...)])
Apart from hardcoding (like below) is there any other way?
for(x1 <- 0 until a.length){
new_a(x1)(0) = a(x1)._1
new_a(x1)(1) = a(x1)._2
new_a(x1)(2) = a(x1)._3
new_a(x1)(3) = a(x1)._4
new_a(x1)(4) = a(x1)._5
}
See http://docs.scala-lang.org/overviews/parallel-collections/conversions.html#converting-between-sequential-and-parallel-collections
val a: ParArray[(Double, Double, Double, Double, Double)] = ???
val rdd = sc.parallelize(a.seq)
Related
I have a piece of code that takes 4 points (which makes up 2 lines), and determines the angle between them. How can I programmatically determine if this is a clockwise rotation or counter-clockwise rotation?
Private Function calculateAngleAlt(L1X1 As Double, L1Y1 As Double, L1X2 As Double, L1Y2 As Double, L2X1 As Double, L2Y1 As Double, L2X2 As Double, L2Y2 As Double) As Double
line1A = L1X2 - L1X1
line1B = L1Y2 - L1Y1
line2A = L2X2 - L2X1
line2B = L2Y2 - L2Y1
lineDot = (line1A * line2A) + (line1B * line2B)
distL1 = Abs(Sqr((line1A * line1A) + (line1B * line1B)))
distL2 = Abs(Sqr((line2A * line2A) + (line2B * line2B)))
calculateAngleAlt = ArcCos(lineDot /(distL1 * distL2))
End Function
Thanks!
I am trying to implement a 2D Perlin Noise in VB.Net. I've spent the whole day searching for sources that explain the subject and one of the most notable was this article by Hugo Elias
Most of the implementation went well. On the exception of a very important part that did not seem to work in VB.Net, causing overflows.
function Noise1(integer x, integer y)
n = x + y * 57
n = (n<<13) ^ n;
return ( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 7fffffff) / 1073741824.0);
end function
In VB.net I translated it to
Private Function Noise(tX As Integer, tY As Integer) As Double
'Return a double between -1 & 1 according to a fixed random seed
Dim n As Integer = tX + tY * 57
n = (n << 13) Xor n
Return (1.0 - ((n * (n * n * 15731 + 789221) + BaseSeed) And &H7FFFFFFF) / 1073741824.0)
End Function
Which cause overflows.
Since the idea seem to be to simply generate a fractional number between -1 and 1. I've made this little function which create a Integer Number based on the coordinates and BaseSeed. BaseSeed(999999) being the base for every noise I'll create in this particular part of my game.
Private Function Noise(tX As Integer, tY As Integer) As Double
Dim tSeed As Integer
tSeed = WrapInteger(789221, BaseSeed, (tX * 1087) + (tY * 2749))
RandomGenerator = New Random(tSeed)
Return (RandomGenerator.Next(-10000, 10001) / 10000)
End Function
WrapInteger simply makes sure that the number will always be in the range of an integer, to avoid overflow errors.
Public Function WrapInteger(ByVal Lenght As Integer, ByVal Position As Integer, ByVal Movement As Integer) As Integer
Lenght += 1
Return ((Position + Movement) + (Lenght * ((Abs(Movement) \ Lenght) + 1))) Mod Lenght
End Function
When I fire it up with a Persistence of 0.25, 6 Octaves and a starting frequency of 2. this is what I get. This is a 128x128 pixel bitmap that I scaled.
Result
Anyone have an idea of why it would be so linear? When I look at this picture I have the feeling that it's not far from the truth, as if it only worked in 1D. All suposition.
Below you will find my entire PerlinNoise Class. I think the rest of it is just fine, but I added it for reference purpose. Beside, I haven't been able to find a single VB implementation of Perlin Noise on the internet. So I guess if I can fix this one, it might help others. There seem to be alot of question about Perlin noise malfunction on StackOverflow
Public Class cdPerlinNoise
Private RandomGenerator As Random
Private BaseSeed As Integer
Private Persistence As Double
Private Frequency As Integer
Private Octaves As Integer
Public Sub New(tSeed As Integer, tPersistence As Double, tOctaves As Integer, tFrequency As Integer)
Frequency = tFrequency
BaseSeed = tSeed
Persistence = tPersistence
Octaves = tOctaves
End Sub
Private Function Noise(tX As Integer, tY As Integer) As Double
Dim tSeed As Integer
tSeed = WrapInteger(789221, BaseSeed, (tX * 1087) + (tY * 2749))
RandomGenerator = New Random(tSeed)
Return (RandomGenerator.Next(-10000, 10001) / 10000)
End Function
Private Function SmoothNoise(tX As Integer, tY As Integer) As Double
Dim Corners As Double = (Noise(tX - 1, tY - 1) + Noise(tX + 1, tY - 1) + Noise(tX - 1, tY + 1) + Noise(tX + 1, tY + 1)) / 16
Dim Sides As Double = (Noise(tX - 1, tY) + Noise(tX + 1, tY) + Noise(tX, tY - 1) + Noise(tX, tY + 1)) / 8
Return (Noise(tX, tY) / 4) + Corners + Sides
End Function
Private Function InterpolateCosine(tA As Double, tB As Double, tX As Double) As Double
Dim f As Double = (1 - Cos(tX * 3.1415927)) * 0.5
Return tA * (1 - f) + tB * f
End Function
Private Function Interpolate2D(tX As Double, tY As Double) As Double
Dim WholeX As Integer = CInt(Fix(tX))
Dim RemainsX As Double = tX - WholeX
Dim WholeY As Integer = CInt(Fix(tY))
Dim RemainsY As Double = tY - WholeY
Dim v1 As Double = SmoothNoise(WholeX, WholeY)
Dim v2 As Double = SmoothNoise(WholeX + 1, WholeY)
Dim v3 As Double = SmoothNoise(WholeX, WholeY + 1)
Dim v4 As Double = SmoothNoise(WholeX + 1, WholeY + 1)
Dim i1 As Double = InterpolateCosine(v1, v2, RemainsX)
Dim i2 As Double = InterpolateCosine(v3, v4, RemainsX)
Return InterpolateCosine(i1, i2, RemainsY)
End Function
Public Function PerlinValue(tX As Double, tY As Double) As Double
Dim Total As Double = 0
Dim Frequency As Double
Dim Amplitude As Double
For i = 0 To Octaves - 1
Frequency = Frequency ^ i
Amplitude = Persistence ^ i
Total = Total + (Interpolate2D(tX * Frequency, tY * Frequency) * Amplitude)
Next
Return Total
End Function
Public Function ScaleNoise(ByVal tX As Double, ByVal tY As Double, ByVal OutputLow As Double, ByVal OutputHigh As Double) As Double
Dim Range1 As Double
Dim Range2 As Double
Dim Result As Double
Range1 = 1 - -1
Range2 = OutputHigh - OutputLow
'(B*C - A*D)/R1 + n1*(R2/R1)
Result = (((1 * OutputLow) - (-1 * OutputHigh)) / Range1) + ((PerlinValue(tX, tY) * (Range2 / Range1)))
If Result < OutputLow Then
Return OutputLow
ElseIf Result > OutputHigh Then
Return OutputHigh
Else
Return Result
End If
End Function
End Class
I have a function:
Public Function F(ByVal a As Double, ByVal b As Double,
ByVal c As Double, ByVal x As Double) As Double
y = ax ^ 3 + bx ^ 2 + cx + d
Return y
End Function
How could I create a function that allows me to read parameters a,b,c,d and return an unary function? For example a=1,b=1,c=3,d=4:
Public Function F(ByVal x As Double) As Double
y = 1x ^ 3 + 2x ^ 2 + 3x + 4
Return y
End Function
or in other words, how could I create a function that returns a function of type
Func(Of Double, Double)
Use a lambda function.
Public Function F(ByVal a As Double, ByVal b As Double, ByVal c As Double) As Func(of Double, Double)
Return Function(ByVal x As Double) As Double
Return ax ^ 3 + bx ^ 2 + cx + d
End Function
End Function
Calling Test1 in Excel gives 0 for any real A and B. Why does this occur?
Public Function Min(X As Double, y As Double, Optional y2 As Double, Optional y3 As Double) As Double
Min = Application.WorksheetFunction.Min(X, y, y2, y3)
End Function
Function Test1(A As Double, B As Double)
Test1 = Min(A, B)
End Function
In Excel: =Test1(5,2).
Optional parameters use their default value if not passed into the function.
For doubles the default value is 0. if you do not provide y2 or y3 essentially, you are testing Application.WorksheetFunction.Min(A,B,0,0).
If you want to change the default value, you can do something like this:
Public Function Min(X As Double, y As Double, Optional y2 As Double = 10000000, Optional y3 As Double = 10000000) As Double
Min = Application.WorksheetFunction.Min(X, y, y2, y3)
End Function
which makes y2 and y3 default to 10000000 which would be your new built in artificial min.
I was wondering what the fastest way is to check for divisibility in VB.NET.
I tried the following two functions, but I feel as if there are more efficient techniques.
Function isDivisible(x As Integer, d As Integer) As Boolean
Return Math.floor(x / d) = x / d
End Function
Another one I came up with:
Function isDivisible(x As Integer, d As Integer) As Boolean
Dim v = x / d
Dim w As Integer = v
Return v = w
End Function
Is this a more practical way?
Use Mod:
Function isDivisible(x As Integer, d As Integer) As Boolean
Return (x Mod d) = 0
End Function
Use 'Mod' which returns the remainder of number1 divided by number2. So if remainder is zero then number1 is divisible by number2.
e.g.
Dim result As Integer = 10 Mod 5 ' result = 0
use the mod operator