I want to write a strategy to evaluate items in an array in parallel. The old strategies had parArr to do this (see here). But this is not found in the new Control.Parallel.Strategies module.
E.g.
parallel list evaluation: map f myList `using` parList rdeepseq
I would want to be able to do something like: amap f myArr `using` parArr rdeepseq, where amap is from Data.Array.Base and applies a function to each of the elements (sequentially).
The following seems to work but I wonder if it is doing it right, and want to know how I could define my own parArr.
This works: amap ((+1) `using` rpar) $ Array.array (0,4) [(0,10),(1,20),(2,30),(3,40),(4,50)]
For a previous question, I wrote a parallel evaluation strategy for the vector package. That should be a good place to start. You can see the code on hackage in the vector-strategies package.
I don't have time to give a full answer - perhaps I'll edit this later. Feel free to comment with extra questions and direction.
Apart from all the good advice given: The reason that there is no parArr anymore is simply that it has been replaced by the more general parTraversable. Just say:
amap f myArr `using` parTraversable rdeepseq
That should give you the behavior you asked for.
Related
I have an array of objects, lets say MyArray[1..x] of Object.
When programming I want to have a more "readable" way of addressing each object. Instead of saying MyArray[1] := ...etc. I would like to say MyReadableName :=...
I've looked into references, but I'm worried a bit about the whole pointer stuff. How could I do this in a good way and support online change? And where should I put the declaration and assignment of the references, it could be many many hundreds, and I don't want to clutter the Main VAR window when in online mode?
Another thing I've looked at is having an enum with the readable names and using this as an index into the array. The lookup is then MyArray[Enum.MyReadableName] :=... but I'm not sure if that is a good solution.
Any solutions or hints are very welcome! Thanks!
You have already mentioned all possible ways for this.
Pointers
You should not be worried of them. Pointers are not a part of IEC-61131 so it's implementation varies from manufacturer to manufacturer. It would be good if you've mention IDE you are using, and a structure of array elements.
The best way how I work with tasks like this, I create ACTION and there I map all variables into arrays or out of arrays. I run this tasks only once on PLC load and call this action Mapping.
VAR
aA: ARRAY [1..2] OF StructureName;
stMyName1: POINTER TO StructureName;
stMyName2: POINTER TO StructureName;
xInit: BOOL;
END_VAR
ACTION actMap
stMyName1 := ADR(aA[1]);
stMyName2 := ADR(aA[2]);
END_ACTION
IF NOT xInit THEN
actMap();
xInit := TRUE;
END_IF
But usually, in IDE actions are created differently, not with ACTION keyword. In Codesys it is right click on POU.
I would go with pointers, because it is mot logical way. It requires little bit more for application setup, but later save time with coding.
Enumeration
This one as you described. In Codesys you should use # like Color#red. But if you make names unique, you can use them without name of enumeration. In addition if you make name of your array short it can looks informative like a[MyArrayName].
Trying to implement something like this:
arr = (1..10)
arr[2,5] = [2,3,4,5]
arr(2,5] = [3,4,5]
arr[2,5) = [2,3,4]
arr(2,5) = [3,4]
Well, we need to override four bracket opreators: [], [), (], ()
Any ideas?
It's called "Including or excluding" in mathematics. https://en.wikipedia.org/wiki/Interval_(mathematics)#Including_or_excluding_endpoints
In short, this is not possible with the current Ruby parser.
The slightly longer answer: You'd have to start by modifying parse.y to support the syntax you propose and recompile Ruby. This is of course not a terrible practical approach, since you'd have to do that again for every new Ruby version. The saner approach would be to start a discussion on ruby-core to see if there is sufficient interest for this to be made part of the language (probably not tbh).
Your wanted syntax is not valid for the Ruby parser, but it could be implemented in Ruby with the help of self-modifying code.
The source files need to be pre-processed. A simple regular expression can substitute your interval expressions with ordinary method syntax, i.e.
arr[2,5] -> interval_closed(arr,2,5)
arr(2,5] -> interval_left_open(arr,2,5)
arr[2,5) -> interval_right_open(arr,2,5)
arr(2,5) -> interval_open(arr,2,5)
The string holding the modified source can be evaluated and becomes part of the application just like a source file on the hard disk. (See instance_eval)
The usage of self-modifying code should be well justified.
Is the added value worth the effort and the complications?
Does the code have to be readable for other programmers?
Is the preprocessing practical? E.g. will this syntax occur in one or a few isolated files, or be spread everywhere?
There is ${name//pattern/string} and ${name:pos:len} for strings but I haven't found any similar documentation for manipulating arrays.
So far I've just been using shift and/or unset for simple manipulation. The only other plausible alternative would be to loop over the values and reconstitute a new array but that isn't desirable.
mksh developer here ;-)
This is not yet implemented currently, to answer your question. It has been on the TODO for a while already, going as far as to be a comment in the source code file in question, so it will probably materialise some day (a good chance to add it is when we’ll have multi-dimensional and associative arrays added, which prompts a partial rewrite of the code in question anyway).
I have a few variables and here they are, three variables "R1, R2 and R3" each have a size of [40 x 1].
I have a fourth variable U of the same dimension. For every U(i) I need to search for an optimum value within R1(i), R2(i) and R3(i) which would return a single value solution. I intend to plot the optimum value against U9i).I have been trying to wrap my head around the knnsearch function but no luck.
Any one out there who could please help??
Thanks
Well when I can't wrap my head around something, I don't come here first.
A lot of people forget this one because we are online, but read a book on the topic. Have your code open so when you see something in the book, test it out.
Draw out any type of diagram. I call these "Napkin Diagrams", because I write it on anything, even a napkin.
I play with code until my keyboard has no letters left on it, then I keep plugging away until the keys fall off
Explore the language API's
Check for public repositories that you can play with
Google, is okay for a quick reference, but google will not teach you anything other than how to google
I talk my code over with myself all the time, people think I'm nuts, but so do I . . It actually works sometimes.
Then if I still can't get it, I come here with a list of things that I have tried, sample code that has not worked, etc.
I used to hate when people told me this, but that was the best thing anyone could have done for me so I tend to do the same now" Thinking about coding is a big part, but u have to get done wht u can. Then we all know what level u are at. Plus it being the end of semester a lot of these types of questions are homework...
Thinking is good, now turn those thoughts into a conceptual design . It's okay to be wrong in this stage, its all just conceptual
If I understood correctly, this might be what you need:
RR = [R1(:) R2(:) R3(:)];
d = bsxfun(#minus,RR, U(:));
[m mi] = min(abs(d),[],2);
answer = RR(:,mi);
first - put the three vectors into a single matrix:
RR = [R1(:) R2(:) R3(:)];
next, take the difference with U: bsxfun is ideal for this kind of thing
d = bsxfun(#minus,RR, U(:));
Now find the minimum absolute difference for each row:
[m mi] = min(abs(d),[],2);
The corresponding indices should allow you to find the "best fit"
answer = RR(:,mi);
I had to do some mind reading to get to this 'answer', so feel free to correct my misunderstanding of your problem!
update if you just need the highest of the three values, then
val = max([R1(:) R2(:) R3(:)]');
plot(U, val);
should be all you need...
I have question relating to implementation of arrays with Java+ANTLR combo. (I'm mainly talking about java/c style arrays).
So basically I'm asking how do you implement such feature, if there is such example already available or if someone could point me to anything that may point to solve it.
On other hand, I've searched a bit how would possible solution be. Main problem that I see
is that user may create arrays of various dimensions, even go crazy if he or she wants (like creating 5 dimension arrays or worse).
While grammar for something like this is fairly simple, like
new ID (INT (',' INT)* )
back end really gets involved a bit. As I said, user may input any number of dimensions, so array dimensions should be dynamically created. (at least as I see it, maybe I'm over complicating things?)
After searching I did found something that pretty much solves this problem perfectly, here is link to the question:
Is it possible to dynamically build a multi-dimensional array in Java?
Of course, my question is, is this viable example, it is a bit (to say at least), complicated? Is there more elegant solution to it?
Having that in mind, I was thinking maybe answer might be in the grounds of somehow transforming multidimensions
into more linear structure ? Could something like that be useful ? Simple search on stackoverflow pointed many solutions
to this, like:
Algorithm to convert a multi-dimensional array to a one-dimensional array
Would it be worth to search in that direction ?
Now, at the end, having in mind that arrays are really common feature in many languages, I must find it surprising that after searching ANTLR mailing list there is no similar question, which as I previously said leads me to believe that I'm maybe over complicating things ? (Unless I really suck at search?) I would really appreciate feedback.
Your syntax, if I'm not mistaken, corresponds to something like
new char 4,5,6,7
which is kind of strange. I expect that you really meant
new char[4,5,6,7]
However from a purely syntactic point of view, there's no reason not to just store the indices in an array and let the semantic analysis pass worry about it.