How to do an in-place `expand` on a domain in Chapel - chapel

The expand command in Chapel returns a new domain. I would like to increase a domain by one kinda like
var d: domain(1) = {1..5};
writeln(d);
--{1..5}
expand(d)(1);
writeln(d);
--{1..6};

As of Chapel 1.15 there is no in-place option for the expand method on domains. You would need to assign the result of expand to the desired domain:
var eDom = {1..5};
eDom = eDom.expand(1);
writeln(eDom); // {0..6}
It doesn't sound like expand is what you want though, because expand will grow the domain in both directions in each dimension. To add one index to a rectangular domain, you can assign a domain literal to your domain:
var rDom = {1..5};
const hi = rDom.last + 1;
rDom = {rDom.first..hi};
writeln(rDom); // {1..6}
For irregular domains you can use the add method:
var aDom = {1, 3, 5, 7}; // an associative domain
aDom.add(9);
writeln(aDom.sorted()); // 1 3 5 7 9
Note that you cannot use the add method on rectangular domains. This is defined in section 19.8.6 in version 0.983 of the Chapel language specification.

A few online experiments on domain expansion:
some worked as documented, some not:
var d: domain(1) = {1..5};
writeln( d ); // SET {1..5}
// {1..5}
var e: domain(1) = d.expand(1);
writeln( e ); // OK, DOMAIN d == {1..5} EXTENDED ON BOTH ENDS INTO {0..6}
// {0..6}
var AonD: [d] int;
AonD.push_back(1);
writeln( AonD.domain ); // OK, DOMAIN EXTENDED INDIRECTLY ON DESIRED END INTO {1..6}
// {1..6}
// var f: domain(1) = {1..5}; // NEW {1..5} - A NON-SHARED, NON-USED ( NON-MAPPED ) DOMAIN
// f.add(6); // FAILS v/s A PROMISE IN: http://chapel.cray.com/docs/master/builtins/internal/ChapelArray.html#ChapelArray.add
// f += 6; // FAILS
// writeln( f );

Related

Updating the values of variables in DAX

I need to change the value of variables in a switch in dax:
switch( true(),
condition1,
var test1 = 2
var test2 = 3
,condition 2,
var test1 = 4
var test2 = 5
,
var test1 = 7
var test2 = 6
)
I need to do this because I have to change massive number of variables depending on the condition, and I don't want to have a switch for every single variable.
I already tried this approach with switch and it works without variables.
It's like dax does not allow you to change the value of a variable after the first time it was assigned.
edit
hello, here is the example of what I want to achieve:
Value Daily Form =
SWITCH( true(),
// if both are selected
(ISFILTERED(tblAAA[AAA_name]) && ISFILTERED(tblBBB[BBB_name])),
var Target = sum(F_Daily_AAA_BBB[target])
var TotalPayments = sum(F_Daily_AAA_BBB[vlr_total_payment])
// if AAA is selected
, (ISFILTERED(tblAAA[AAA_name]) && not(ISFILTERED(tblBBB[BBB_name]))),
var Target = sum(F_Daily_AAA[target])
var TotalPayments = sum(F_Daily_AAA[vlr_total_payment])
// if BBB is selected
, (not(ISFILTERED(tblAAA[AAA_name])) && ISFILTERED(tblBBB[BBB_name])),
var Target = sum(F_Daily_BBB[target])
var TotalPayments = sum(F_Daily_BBB[vlr_total_payment])
// none is selected
,
var Target = sum(F_Daily_OR[target])
var TotalPayments = sum(F_Daily_OR[vlr_total_payment])
)
var result =
SWITCH(FIRSTNONBLANK(tblKPIs[Group_Name], tblKPIs[Group_Name]),
"Target (€)", IF(Target > 0, FORMAT(Target, "€ #,##"), if(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
"Total Payments (€)", IF(TotalPayments > 0, FORMAT(TotalPayments, "€ #,##"), IF(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
)
return result
in the beginning the code that I have to modify is:
Value Daily Form =
var Target = sum(F_Daily_OR[target])
var TotalPayments = sum(F_Daily_OR[vlr_total_payment])
var result =
SWITCH(FIRSTNONBLANK(tblKPIs[Group_Name], tblKPIs[Group_Name]),
"Target (€)", IF(Target > 0, FORMAT(Target, "€ #,##"), if(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
"Total Payments (€)", IF(TotalPayments > 0, FORMAT(TotalPayments, "€ #,##"), IF(SELECTEDVALUE('Calendar'[isWorkingDay]) = 1, "€ 0", BLANK())),
)
return result
The only difference is the instead of 2 variables there are like 75,
and I don't want to make a switch for each of them because this will spaghettify the code.
Also I have to modify like 50 of this Metrics full of variables, so I need a quick copy paste solution like that switch, which worked in other cases in which the code didn't have variables.
Thank you for taking the time to help me.
It is indeed, because variables in DAX are actually constants. Variables are immutable. You can store the value in variable but you cannot change it later.
Here is a definition of the DAX variable from the documentation:
Stores the result of an expression as a named variable, which can then be passed as an argument to other measure expressions. Once resultant values have been calculated for a variable expression, those values do not change, even if the variable is referenced in another expression.
Find more in the documentation.
What exactly do you want to achieve by this?
Creating separate measure for each condition and then another measure with switch condition won't work for you?

How to generate third field in _ga and _gid?

I'm investigate too much on _gid and _ga. And as i know, the definition of them:
_ga: used to identify unique users and it expires after 2 years.
_gid: used to identify unique users and it expires after 24 hours.
Example:
_ga: GA1.3.292651669.1502954402
_gid: GA1.3.974792242.1509957189
From what are the values in _ga cookie?, I know the meaning of each filed in _ga. And it's the same for _gid.
But I don't know how to generate third field, random generated user ID(for _ga:292651669 and for _gid: 974792242).
I tried to delete both _ga & _gid and I get the new couple _ga: GA1.3.2097663971.1509959880 and _gid: GA1.3.1180999143.1509959880. The third fields of both are changed. So how can they generate and how google identify user by them. Assumption I open browser today with a couple of _gid and _ga, and tomorrow, I cleared all cookies, ga will create of new couplw (_gid,_ga).; It means I'm in today that different tomorrow's me.
Please help me.
Thanks & Best Regards,
Jame
Here is the code from analytics.js release 2017-09-21 that generates the random id.
It's a random value based on current time, userAgent, cookie, referrer and history.length from the window object.
var O = window;
// var M = document;
// Stack Overflow doesn't allow cookie access in sandbox, so lets fake it:
var M = [];
M.cookie = "cookieName=someValue";
var hd = function() {
return Math.round(2147483647 * Math.random())
}
function La(a) {
var b = 1, c;
if (a)
for (b = 0,
c = a.length - 1; 0 <= c; c--) {
var d = a.charCodeAt(c);
b = (b << 6 & 268435455) + d + (d << 14);
d = b & 266338304;
b = 0 != d ? b ^ d >> 21 : b
}
return b
}
var ra = function() {
for (var a = O.navigator.userAgent + (M.cookie ? M.cookie : "") + (M.referrer ? M.referrer : ""), b = a.length, c = O.history.length; 0 < c; )
a += c-- ^ b++;
return [hd() ^ La(a) & 2147483647, Math.round((new Date).getTime() / 1E3)].join(".")
}
document.getElementById('output').innerText = ra();
<h3>Output:</h3>
<pre id="output"></pre>

Print string domain values with comma separation in Chapel

I have a domain I'd like to output with commas. In Python I can use the string .join() method, being fed by a list .sort()-ed product, but in Chapel I am not getting the right results.
var names = { "anze kopitar",
"tyler toffoli",
"drew doughty",
"jeff carter",
"tanner pearson"
};
writeln( names );
writeln( names.sorted() );
writeln( ",".join( names ) );
writeln( ",".join( names.sorted() ) );
I'd like the last line to read
anze kopitar,drew doughty,jeff carter,tanner pearson,tyler toffoli
In Chapel 1.16, string.join only supports varargs, tuples, and arrays as arguments. For now you will need to convert your domain to one of those types before joining:
var dom = {"apple", "orange", "carrot"};
var A = dom.sorted(); // 'A' is an array
const s = ",".join(A);
writeln(s);
The output is:
apple,carrot,orange

Can one create an object to store multiple domains?

I have some code which I think should look like:
on Locales[0] {
var slice: domain(1) = {0..#widthOfLocaleMatrix};
on Locales[1] {
slice(0) = A.localSubdomain();
}
var localSlice: [slice(0)] int = A[slice(0)];
}
Basically, I am trying to fetch multiple slices of data from the other numLocales - 1 locales. Can I create an object to store the localSubdomain's from all other locales? I think I can work around this, but I was curious.
To store multiple domains, you'll want to create an array of domains (or some other collection of domains). Specifically, the main problem with the code above is that it is seemingly trying to index into a domain ( slice(0) ) -- keep in mind that domains are merely index sets, not arrays/maps from indices to values.
The following sample program creates a distributed array ( A ) whose distribution we want to interrogate and an array of domains ( slicePerLocale ) that we'll use to keep track of who owns what. It populates slicePerLocale via the localSubdomain() query to determine the subdomain that each locale owns and stores that in the respective element of slicePerLocale. Finally, it prints out what it has learned:
use BlockDist;
config const n = 10;
var D = {1..n, 1..n} dmapped Block({1..n, 1..n});
var A: [D] real;
var slicePerLocale: [LocaleSpace] domain(2);
coforall loc in Locales do
on loc do
slicePerLocale[loc.id] = A.localSubdomain();
for (loc, slice) in zip(LocaleSpace, slicePerLocale) do
writeln("locale ", loc, " owns: ", slice);
Running this on four locales with the default problem size of 10 results in:
locale 0 owns: {1..5, 1..5}
locale 1 owns: {1..5, 6..10}
locale 2 owns: {6..10, 1..5}
locale 3 owns: {6..10, 6..10}

How do I create a list with no predefined length and assigned values to their positions, in Dart?

I'm developing my first application with Dart, which had previously created in JavaScript.
In my statement in JavaScript I have declared a List, and I assign values ​​to the first three positions, as seen here:
serpF var = new List ();
serpF [0] = 10;
serpF [1] = 10;
serpF [2] = 10;
How I can do the same in Dart? I have read the documentation of Lists and Arrays of Seth Ladd in "http://blog.sethladd.com/2011/12/lists-and-arrays-in-dart.html" and I've tried everything, but it is being impossible.
The javaScript code does not work in Dart because it is an error to access past the end of the List. Dart is like many other languages as this is a way to catch logic errors.
What you can do is add elements to a List:
var serpF = new List(); // This list has length == 0.
serpF.add(10);
serpF.add(10);
serpF.add(10);
You can use method cascades to shorten the code:
var serfP = [];
serpF..add(10)..add(10)..add(10);
If you do know the length, you might also try one of the other List constructors:
var serfP = new List.filled(3, 10);
var serfP = new List.generate(3, (i) => 10);
I addition to what Stephen suggests you can also do:
var serpF = [10, 10, 10];
or
var serpF = new List();
serpF.length = 3;
serpF[0] = 10;
serpF[1] = 10;
serpF[2] = 10;
You can also use the List.generate() constructor:
var list = new List.generate(3, (_) => 10, growable: true);
print(list); // Prints [10, 10, 10].
The first arg specifies the number of elements in the list, the second arg takes a callback to give each element a value, and the third arg ensures that the list is not fixed-width.