How to do an inline IF array constructor in Chapel? - list-comprehension

I'd like to create a subset of a domain based on a conditional. I could do a loop, but I'm looking to see if I can use an inline if.
Just re-creating the array d looks like
var d = {1..8};
var e = [0.875, 0.625, 0.625, 1.0, 0.625, 0.875, 0.625, 0.625];
var p = 0.7;
var vs = for i in d do i;
writeln(" vs: ", vs);
However, I want to extract the d where e[d] < p into vs. Is there an approach like?
vs = [i in d where e[i] < p]
writeln(vs); // {2,3,5,7,8}

This should give you the desired result:
var vs = for i in d do
if e[i] < p then i;
Note that vs is an array and not a domain. If you want a domain that you can use, you should use an associative domain:
var vs : domain(int) = for i in d do
if e[i] < p then i;
This example will turn into something like this:
var vs : domain(int);
for i in d {
if e[i] < p then
vs.add(i);
}

Related

Can Jarvis marching algorith return UNso0rted results?

I am trying to compute a complex hull for a very complex point set. See picture:
The green/teal line to the left contains convex hull points that are neighboring, i.e. come directly after one another. continuing the sequence, the next point should be the cyan/green one in right bottom.
However, if i walk along the array, the next one appears to be the one in top right, in yellow.
Yes, output is in ascii, point cloud is correct (compared with GUI output) .
Snippet:
for(int i = 0; i< rd.length; i++) {
if( (*rd)[i].lon < lonMin) {
lonMin = (*rd)[i].lon;
leftMostPos = i;
}
}
int [] hullIdx = new int[0];
int p = leftMostPos, m = to!int((*rd).length), q;
do {
hullIdx ~= [p];
q = (p + 1) % m;
for ( int ij = 0; ij < m ; ij ++) {
auto a = (*rd)[p];
auto b = (*rd)[ij];
auto c = (*rd)[q];
double ornt = (b.lat - a.lat) * ( c.lon - b.lon) - (b.lon - a.lon) * (c.lat - b.lat) ;
if (ornt < 0) q = ij;
}
p = q;
} while ( p != leftMostPos);
Language is D, I am solving for Geographical Lats and Lons. Lons are taken as a coordinates, lats are in x coordinate. The Matrix increases y coordinate by row, towards downward direction. This is also taken into account.
I would like to know how come Jarvis algorithm returns UNsorted output. I would apppreciate all your help with gratefulness.

NodeJS Zlib support for LZW ("compress") algorithm?

I've got a Node/ExpressJS server whose client software only has access to the "compress" (LZW) algorithm for de/compression.
As far as I can tell the Node 12.X zlib library does not support LZW. There also don't appear to be any modules in npm that handle LZW in a fast, general way on content larger than a few hundred bytes.
Does anyone know of a way to efficiently and, ideally, natively use LZW on a Node server? Is something in Zlib compatible with LZW? My use case is for data up to a few tens of kilobytes.
Everything is in Docker, so I could install ncompress on the host and use child_process to call it directly, or something, but that seems convoluted.
You could do this without any external library.
These are two LZW encode and decode functions.
function en(c) { var x = "charCodeAt", b, e = {}, f = c.split(""), d
= [], a = f[0], g = 256; for (b = 1; b < f.length; b++) c = f[b], null != e[a + c] ? a += c :(d.push(1 < a.length ? e[a] :ax),
e[a + c] = g, g++, a = c); d.push(1 < a.length ? e[a] :ax);
for (b = 0; b < d.length; b++) d[b] = String.fromCharCode(d[b]);
return d.join(""); }
function de(b) { var a, e = {}, d = b.split(""), c = f = d[0], g = [
c ], h = o = 256; for (b = 1; b < d.length; b++) a =
d[b].charCodeAt(0), a = h > a ? d[b] :e[a] ? e[a] :f + c,
g.push(a), c = a.charAt(0), e[o] = f + c, o++, f = a; return
g.join(""); }
Please note: These functions are for strings only.
Source: https://gist.github.com/JavaScript-Packer/bbf68a4dc0e1fd102221

Color interpolation with a set of 2D points

I am trying to modelize a physical problem of photoelasticity on a surface. I succeed to get an array of X,Y - coordinates of the surface and for each points I have a corresponding color in a RGB format. I used Python scatter to plot and the result is already great but there is still some discontinuities because of the lack of resolution that I can not improve :( I just wanted to ask how I can generate the same surface plot in a continuous way (with new "in-between" points for which the color of them have been interpolated with respect to the neighborhood points). I am not necessarily looking for coding it in python, every software is welcome. Thanks!
I wrote this in javascript: https://jsfiddle.net/87nw05kz/
The important part is calculateColor. It finds the inverse square of the distance to each color from the pixel being shaded and it uses that to decide how much that pixel should be effected by each color.
function calculateColor(x, y) {
let total = 0;
for (let i = 0; i < colors.length; i++) {
let c = colors[i];
let d = distance(c.x, c.y, x, y);
if (d === 0) {
return c;
}
d = 1 / (d * d);
c.d = d;
total += d;
}
let r = 0, g = 0, b = 0;
for (let i = 0; i < colors.length; i++) {
let c = colors[i];
let ratio = c.d / total;
r += ratio * c.r;
g += ratio * c.g;
b += ratio * c.b;
}
r = Math.floor(r);
g = Math.floor(g);
b = Math.floor(b);
return {r:r,g:g,b:b};
}

basic fractal coloring problems

I am trying to get more comfortable with the math behind fractal coloring and understanding the coloring algorithms much better. I am the following paper:
http://jussiharkonen.com/files/on_fractal_coloring_techniques%28lo-res%29.pdf
The paper gives specific parameters to each of the functions, however when I use the same, my results are not quite right. I have no idea what could be going on though.
I am using the iteration count coloring algorithm to start and using the following julia set:
c = 0.5 + 0.25i and p = 2
with the coloring algorithm:
The coloring function simply returns the number of
elements in the truncated orbit divided by 20
And the palette function:
I(u) = k(u − u0),
where k = 2.5 and u0 = 0, was used.
And with a palette being white at 0 and 1, and interpolating to black in-between.
and following this algorithm:
Set z0 to correspond to the position of the pixel in the complex plane.
Calculate the truncated orbit by iterating the formula zn = f(zn−1) starting
from z0 until either
• |zn| > M, or
• n = Nmax,
where Nmax is the maximum number of iterations.
Using the coloring and color index functions, map the resulting truncated
orbit to a color index value.
Determine an RGB color of the pixel by using the palette function
Using this my code looks like the following:
float izoom = pow(1.001, zoom );
vec2 z = focusPoint + (uv * 4.0 - 2.0) * 1.0 / izoom;
vec2 c = vec2(0.5f, 0.25f) ;
const float B = 2.0;
float l;
for( int i=0; i<100; i++ )
{
z = vec2( z.x*z.x - z.y*z.y, 2.0*z.x*z.y ) + c;
if( length(z)>10.0) break;
l++;
}
float ind = basicindex(l);
vec4 col = color(ind);
and have the following index and coloring functions:
float basicindex(float val){
return val / 20.0;
}
vec4 color(float index){
float r = 2.5 * index;
float g = r;
float b = g;
vec3 v = 0.5 - 0.5 * sin(3.14/2.0 + 3.14 * vec3(r, g, b));
return vec4(1.0 - v, 1.0) ;
}
The paper provides the following image:
https://imgur.com/YIZMhaa
While my code produces:
https://imgur.com/OrxdMsN
I get the correct results by using k = 1.0 instead of 2.5, however I would prefer to understand why my results are incorrect. When extending this to the smooth coloring algorithms, my results are still incorrect so I would like to figure this out first.
Let me know if this isn't the correct place for this kind of question and I can move it to the math stack exchange. I wasn't sure which place was more appropriate.
Your image is perfectly implemented for Figure 3.3 in the paper. The other image you posted uses a different routine.
Your figure seems to have that bit of perspective code there at top, but remove that and they should be the same.
If your objection is the color extremes you set that with the "0.5 - 0.5 * ..." part of your code. This makes the darkest black originally 0.5 when in the example image you're trying to duplicate the darkest black should be 1 and the lightest white should be 0.
You're making the whiteness equal to the distance from 0.5
If you ignore the fractal all together you are getting a bunch of values that can be normalized between 0 and 1 and you're coloring those in some particular ways. Clearly the image you are duplicating is linear between 0 and 1 so putting black as 0.5 cannot be correct.
o = {
length : 500,
width : 500,
c : [.5, .25], // c = x + iy will be [x, y]
maxIterate : 100,
canvas : null
}
function point(pos, color){
var c = 255 - Math.round((1 + Math.log(color)/Math.log(o.maxIterate)) * 255);
c = c.toString(16);
if (c.length == 1) c = '0'+c;
o.canvas.fillStyle="#"+c+c+c;
o.canvas.fillRect(pos[0], pos[1], 1, 1);
}
function conversion(x, y, R){
var m = R / o.width;
var x1 = m * (2 * x - o.width);
var y2 = m * (o.width - 2 * y);
return [x1, y2];
}
function f(z, c){
return [z[0]*z[0] - z[1] * z[1] + c[0], 2 * z[0] * z[1] + c[1]];
}
function abs(z){
return Math.sqrt(z[0]*z[0] + z[1]*z[1]);
}
function init(){
var R = (1 + Math.sqrt(1+4*abs(o.c))) / 2,
z, x, y, i;
o.canvas = document.getElementById('a').getContext("2d");
for (x = 0; x < o.width; x++){
for (y = 0; y < o.length; y++){
i = 0;
z = conversion(x, y, R);
while (i < o.maxIterate && abs(z) < R){
z = f(z, o.c);
if (abs(z) > R) break;
i++;
}
if (i) point([x, y], i / o.maxIterate);
}
}
}
init();
<canvas id="a" width="500" height="500"></canvas>
via: http://jsfiddle.net/3fnB6/29/

find whether given (x,y) coordinate exists in SVG Image?

I wanted to find whether a given point exists in image area. Image width and height is 40. The four corners are (10,10),(50,10),(10,50),(50,50). Is there a way to find a given (x,y) exists or not in the area?
to check whether point lies inside the rect or not, i uses line equation and compare the point with all four lines.
The key to this approach is the sequence of points.
check the below code:-
function isPointInside(fourPointsArray, pointToCheck) {
var counter = 0;
var ele = pointToCheck;
var A, B, C, D, p1, p2, indx;
for (var k = 0; k < fourPointsArray.length; k++) {
p1 = fourPointsArray[k];
indx = k + 1;
if (indx >= fourPointsArray.length)
indx = 0;
p2 = fourPointsArray[indx];
A = -(p2.y - p1.y);
B = p2.x - p1.x;
C = -(A * p1.x + B * p1.y);
D = A * ele.x + B * ele.y + C;
if (D >= 0) {
counter++;
}
}
if (counter >= fourPointsArray.length) {
return true;
}
return false;
}
var arraypoints=[{x:10,y:10},{x:50,y:10},{x:50,y:50},{x:10,y:50}];
var pointtocheck={
x:5,
y:10
}
var flag=isPointInside(arraypoints,pointtocheck);
console.log(flag);

Resources