mapbox gl calculate inside expression for circle-radius - expression

I'd like to do a calculation inside an expression. Is this possbile?
The code I have for now doesn't work. This example works fine:
'circle-radius': {
'base': 1.5,
'stops': [
[0, 0],
[20, 180]
]
}
But when I do a calculation inside, it doesnt work:
'circle-radius': {
'base': 1.5,
'stops': [
[0, 0],
[20, ['*', 2, 90]]
]
},
And what I'm really trying to do in the end is adding another variable from my data.
'circle-radius': {
'base': 1.5,
'stops': [
[0, 0],
[20, ['*', 2, ["get", "amount"]]]
]
},
Thanks!

You're mixing two different Mapbox-GL syntaxes together: the older "stops" method, and the newer expression syntax. Since you want to do a calculation, you need to entirely use the new expression syntax.
Something like this:
{
"circle-radius": [
"interpolate", ["linear"], ["zoom"],
0, 0,
20, ['*', 2, ['get', 'amount']]]
]
}

Related

I'm trying to use elasticsearch aggregation generate array having sum of each element of array

I have documents that look like:
{
"arr": [5, 4, 3, 2, 1],
"name": "test"
}
{
"arr": [4, 3, 2, 1, 0],
"name": "test"
}
{
"arr": [1, 0, 0, 0, 0],
"name": "test"
}
I want to use an aggregation (or some other es method) to return:
{
"arr": [10, 7, 5, 3, 1]
}
Elasticsearch doesn't "properly" store lists; in your case internal representation of such field would be arr = 5 AND arr = 4 AND .... Though if all docs have 5 items (or less) you can do something like:
{
"arr": [5, 4, 3, 2, 1],
"arr_0": 5,
"arr_1": 4,
"arr_2": 3,
"arr_3": 2,
"arr_4": 1,
"name": "test"
}
and then 5 sum aggregations over arr_0 ... arr_4.
If there is no limit on the array length, you'll have to compute in your app I'm afraid.

tf.diag of a 2D matrix

I have a 2D tensor with various arrays defined as:
x = tf.constant([[0,1,2],[-1,0,1],[-1,-2,0]])
and I want to convert each array to a diagonal matrix as:
diag_x =
[[[ 0, 0, 0],
[ 0, 1, 0],
[ 0, 0, 2]],
[[-1, 0, 0],
[ 0, 0, 0],
[ 0, 0, 1]],
[[-2, 0, 0],
[ 0, -1, 0],
[ 0, 0, 0]]]
but if I use the operation tf.diag(x) the output is not this.
I finally found the solution:
tf.matrix_diag(x)
EDIT: For TF 2.0, you can use
tf.linalg.diag(x)
You can try:
tf.matrix_set_diag(tf.zeros((3,3,3), dtype=tf.int32), x)

OpenSCAD how to access a value within a Matrix

How do I index a matrix in OpenSCAD or iterate through it in a loop?
I'm trying to either access and assign the values assigned to coordinates through the forloop to their single variables as below, or at least be able to access the values separately in the Matrix.
for ( coordinates = [ [ 15, 15, 2],
[ 15, -15, 2],
[ -15, -15, 2],
[ -15, 15, 2] ])
{
x = coordinates[0];
y = coordinates[1];
z = coordinates[2];
translate([x+4, y, z]){
cube([x,y,z]);
}
}
First off, standard variables are set at compile-time in OpenSCAD, not run-time (official documentation stating that), so you can't assign values to them in a loop. You'll have to inline references to coordinates to use the values in it.
The second issue is that you can't make a cube with a negative size, or so I'm guessing from the fact that I get no output from the second through fourth iterations of the loop as provided. You can wrap the values passed into cube in abs() calls to get the absolute value to ensure it's positive.
Here's a working sample of inlining the coordinates variable and using abs() to pass positive values to cube():
for ( coordinates = [ [ 15, 15, 2],
[ 15, -15, 2],
[ -15, -15, 2],
[ -15, 15, 2] ])
{
translate([coordinates[0] + 4, coordinates[1], coordinates[2]]) {
cube([abs(coordinates[0]), abs(coordinates[1]), abs(coordinates[2])]);
}
}

YARVInstructionSequence/SimpleDataFormat to Source conversion

A proc can be converted in to Ruby virtual machine sequences in YARVInstructionSequence/SimpleDataFormat as follows:
RubyVM::InstructionSequence.of(->x{x + 1}).to_a
# => ["YARVInstructionSequence/SimpleDataFormat", 2, 0, 1,
{:arg_size=>1, :local_size=>2, :stack_max=>2},
"block in irb_binding", "(irb)", nil, 2,
:block, [:x], [1, [], 0, 0, -1, -1, 3],
[
[:redo, nil, :label_0, :label_9, :label_0, 0],
[:next, nil, :label_0, :label_9, :label_9, 0]
],
[
:label_0, 2, [:trace, 256], [:trace, 1], [:getlocal_OP__WC__0, 2],
[:putobject_OP_INT2FIX_O_1_C_],
[:opt_plus, {:mid=>:+, :flag=>256, :orig_argc=>1, :blockptr=>nil}], :label_9,
[:trace, 512], [:leave]
]
]
Is there a way to convert this format back to reconstruct the source expression in string like "->x{x+1}" or something close to it?
The format is explained in RDoc as follows:

How can I save an object to a file?

I would like to save an object to a file, and then read it from the file easily. As a simple example, lets say I have the following 3d array:
m = [[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]]
Is there an easy Ruby API that I can use to achieve this without programming a parser to interpret the data from the file? In the example I give it is easy, but as the objects become more complicated, it gets annoying to make objects persistent.
You need to serialize the objects before you could save them to a file and deserialize them to retrieve them back. As mentioned by Cory, 2 standard serialization libraries are widely used, Marshal and YAML.
Both Marshal and YAML use the methods dump and load for serializing and deserializing respectively.
Here is how you could use them:
m = [
[
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
],
[
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
]
# Quick way of opening the file, writing it and closing it
File.open('/path/to/yaml.dump', 'w') { |f| f.write(YAML.dump(m)) }
File.open('/path/to/marshal.dump', 'wb') { |f| f.write(Marshal.dump(m)) }
# Now to read from file and de-serialize it:
YAML.load(File.read('/path/to/yaml.dump'))
Marshal.load(File.read('/path/to/marshal.dump'))
You need to be careful about the file size and other quirks associated with File reading / writing.
More info, can of course be found in the API documentation.
See Marshal: http://ruby-doc.org/core/classes/Marshal.html
-or-
YAML: http://www.ruby-doc.org/core/classes/YAML.html
YAML and Marshal are the most obvious answers, but depending on what you're planning to do with the data, sqlite3 may be a useful option too.
require 'sqlite3'
m = [[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]]
db=SQLite3::Database.new("demo.out")
db.execute("create table data (x,y,z,value)")
inserter=db.prepare("insert into data (x,y,z,value) values (?,?,?,?)")
m.each_with_index do |twod,z|
twod.each_with_index do |row,y|
row.each_with_index do |val,x|
inserter.execute(x,y,z,val)
end
end
end

Resources