Related
I was trying to display intrabar close values inside the current chart's bar(15 min). So there are three values. But the values comes as tuple.
I cannot separate the tuples to three values. I have tried the below code as well as using for loop(which I am not familiar with),
[fir, sec, thi] = request.security_lower_tf(syminfo.tickerid, "5", close)
var table top_boxes = table.new(position.bottom_center, 6, 2)
table.cell(top_boxes, 0, 0, text=str.tostring(a), bgcolor=color.new(color.blue, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 1, 0, text=str.tostring(b), bgcolor=color.new(color.red, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 1, 0, text=str.tostring(c), bgcolor=color.new(color.yellow, 0), text_color=color.white, width=4, height=8)
Can someone help me to separate the values in the array and display them.
Since you only request close price, request.security_lower_tf() function returns an array with 3 elements.
You can get the value of each element using array.get():
close_5 = request.security_lower_tf(syminfo.tickerid, "5", close)
var table top_boxes = table.new(position.bottom_center, 6, 2)
if barstate.islast
table.cell(top_boxes, 0, 0, text=str.tostring(array.get(close_5, 0)), bgcolor=color.new(color.blue, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 1, 0, text=str.tostring(array.get(close_5, 1)), bgcolor=color.new(color.red, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 2, 0, text=str.tostring(array.get(close_5, 2)), bgcolor=color.new(color.yellow, 0), text_color=color.white, width=4, height=8)
In some cases, there will be missing some 5 min bars in order to return 3 elements into that array. You can write a "safer" code using a for loop:
//#version=5
indicator("request 5 min timeframe", overlay = true)
close_5 = request.security_lower_tf(syminfo.tickerid, "5", close)
var table top_boxes = table.new(position.bottom_center, 6, 2)
if barstate.islast
color[] cell_bg_color = array.from(color.blue, color.red, color.yellow)
for [index, price] in close_5
table.cell(top_boxes, index, 0, text=str.tostring(price), bgcolor=array.get(cell_bg_color, index), text_color=color.white)
I'm coding in React-Native.
Glympse docs says that location data should be sent in a delta-compressed array. I don't really know what that means. I think I get the idea of each element being the amount of change (delta) from the previous element, but I still don't have a clear picture of how the body should look when I make the POST request.
Can anyone show an example of this process?
Examples of location arrays that are compressed can be found here https://developer.glympse.com/docs/core/api/reference/objects/location-points#examples
The idea behind this format is that the first item in the array contains specific values for each parameter, but each item that comes after that only contains the change (or delta) from the previous point.
[
[1339989715000, 37123450, -112123450, 18000, 55, null, 2, 4],
[1000, 1000000, 1000000, 0, null, 1000, 1, -1],
[1000, 0, 0, 0, 1, 0, 0, 0],
[1000, 0, 0, 0, 0, 0, 0, 0],
[1000, 0, 0, 0, 0, 0, 0, 0]
]
The first parameter is the timestamp, so if we look at the second item it shows 1000 which means it's the first timestamp + 1000ms.
The second parameter is latitude * 10^6. The first item shows latitude 37.123450, and the second item in the array has the value 1000000 which represents 37123450 + 1000000 or the latitude 38.123450. Not likely to have something moving that fast in real data, but that's the idea of how this format works.
Timestamp, latitude, and longitude are the only required fields. A POST body with only the required fields would look like this.
[
[1339989715000, 37123450, -112123450],
[1000, 1000000, 1000000],
[1000, 0, 0],
[1000, 0, 0],
]
So my question is simply,
given
var myArray = new[]{new []{0, 0, 7, 0},
new []{0, 0, 0, 0},
new []{0, 0, 0, 0},
new []{0, 0, 0, 0}};
how do I change the value that is 7 to 0?
i know to index a single array I would use myArray[2] = 0
I just dont know how to index a value in an array of arrays.
is it something like this, myArray[0[2]]?
any help would be greatly appreciated.
Very close. Think about the object myArray[0] returns - It's an array itself, yes?
And to access the 3rd element in the first list, we just do myArray[0][2]. Essentially, we are indexing the 2nd item in the first item of myArray
If I have an array of array (similar to a matrix) in Scala, what's the efficient way to sum up each column of the matrix? For example, if my array of array is like below:
val arr = Array(Array(1, 100, ...), Array(2, 200, ...), Array(3, 300, ...))
and I want to sum up each column (e.g., sum up the first element of all sub-arrays, sum up the second element of all sub-arrays, etc.) and get a new array like below:
newArr = Array(6, 600, ...)
How can I do this efficiently in Spark Scala?
There is a suitable .transpose method on List that can help here, although I can't say what its efficiency is like:
arr.toList.transpose.map(_.sum)
(then call .toArray if you specifically need the result as an array).
Using breeze Vector:
scala> val arr = Array(Array(1, 100), Array(2, 200), Array(3, 300))
arr: Array[Array[Int]] = Array(Array(1, 100), Array(2, 200), Array(3, 300))
scala> arr.map(breeze.linalg.Vector(_)).reduce(_ + _)
res0: breeze.linalg.Vector[Int] = DenseVector(6, 600)
If your input is sparse you may consider using breeze.linalg.SparseVector.
In practice a linear algebra vector library as mentioned by #zero323 will often be the better choice.
If you can't use a vector library, I suggest writing a function col2sum that can sum two columns -- even if they are not the same length -- and then use Array.reduce to extend this operation to N columns. Using reduce is valid because we know that sums are not dependent on order of operations (i.e. 1+2+3 == 3+2+1 == 3+1+2 == 6) :
def col2sum(x:Array[Int],y:Array[Int]):Array[Int] = {
x.zipAll(y,0,0).map(pair=>pair._1+pair._2)
}
def colsum(a:Array[Array[Int]]):Array[Int] = {
a.reduce(col2sum)
}
val z = Array(Array(1, 2, 3, 4, 5), Array(2, 4, 6, 8, 10), Array(1, 9));
colsum(z)
--> Array[Int] = Array(4, 15, 9, 12, 15)
scala> val arr = Array(Array(1, 100), Array(2, 200), Array(3, 300 ))
arr: Array[Array[Int]] = Array(Array(1, 100), Array(2, 200), Array(3, 300))
scala> arr.flatten.zipWithIndex.groupBy(c => (c._2 + 1) % 2)
.map(a => a._1 -> a._2.foldLeft(0)((sum, i) => sum + i._1))
res40: scala.collection.immutable.Map[Int,Int] = Map(2 -> 600, 1 -> 6, 0 -> 15)
flatten array and zipWithIndex to get index and groupBy to map new array as column array, foldLeft to sum the column array.
I create zeroed Arrays in Scala with
(0 until Nrows).map (_ => 0).toArray but is there anything faster ? map is slow.
I have the same question but with 1 instead of O, i.e. I also want to accelerate (0 until Nrows).map (_ => 1).toArray
Zero is the default value for an array of Ints, so just do this:
val array = new Array[Int](NRows)
If you want all those values to be 1s then use .fill() (with thanks to #gourlaysama):
val array = Array.fill(NRows)(1)
However, looking at how this works internally, it involves the creation of a few objects that you don't need. I suspect the following (uglier) approach may be quicker if speed is your main concern:
val array = new Array[Int](NRows)
for (i <- 0 until array.length) { array(i) = 1 }
For multidimensional arrays consider Array.ofDim, for instance,
scala> val a = Array.ofDim[Int](3,3)
a: Array[Array[Int]] = Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0))
Likewise,
scala> val a = Array.ofDim[Int](3)
a: Array[Int] = Array(0, 0, 0)
In the context here,
val a = Array.ofDim[Int](NRows)
For setting (possibly nonzero) initial values, consider Array.tabulate, for instance,
scala> Array.tabulate(3,3)( (x,y) => 1)
res5: Array[Array[Int]] = Array(Array(1, 1, 1), Array(1, 1, 1), Array(1, 1, 1))
scala> Array.tabulate(3)( x => 1)
res18: Array[Int] = Array(1, 1, 1)