MapThread, Manipulate, Filter in Mathematica - select

I hope to be able to name that question properly soon.
Please Consider :
list1 = Tuples[Range[1, 5], 2];
list2 = Tuples[Range[3, 7], 2];
*I use the below mechanism to display all filtered eye fixations during a display. *
Manipulate[Row[
MapThread[Function[{list},
Graphics[
Point[{#[[1]], #[[2]]}]& /# Select[list,
(#[[1]] > f1 && #[[2]] > f2) &],
Frame -> True, PlotRange -> {{0, 10}, {0, 10}}]],
{{list1, list2}}]],
{f1, 0, 10}, {f2, 0, 10}]
Now, I would like to display each fixation (point) one at a time, cumulatively.
That is :
Given
list1 = {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 2}, {2, 3}, {2, 4},
{2, 5}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}, {4, 1}, {4, 2}, {4, 3},
{4, 4}, {4, 5}, {5, 1}, {5, 2}, {5, 3}, {5, 4}, {5, 5}}
Use a slider to display the 1 to 25 Points here. But after filter the 1 to Length#Filtered Data
The Slider that control the Fixation number has yet a fixed boundary (25) , whereas it should have one equal to the Length of the filtered list.
But there is 2 due to Mapthread.
And I cannot extend the Mapthread to the Manipulate Control, could I ?
Manipulate[Row[MapThread[Function[{list},
Graphics[
Point[{#[[1]], #[[2]]}]& /# Select[list,
(#[[1]] > f1 && #[[2]] > f2) &]
[[1 ;; dd, All]],
Frame -> True, PlotRange -> {{0, 10}, {0, 10}}]],
{{list1, list2}}]],
{f1, 0, 10}, {f2, 0, 10},{dd,0,25}]

Perhaps something like:
(Beware of code efficiency)
list1 = Tuples[Range[1, 5], 2];
list2 = Tuples[Range[3, 7], 2];
f = (Select[#, (#[[1]] > f1 && #[[2]] > f2) &] &);
Manipulate[
Row#Graphics[Point##, Frame -> True, PlotRange -> {{0, 10}, {0, 10}}] & /#
Map[Map[f, {#}][[All, 1 ;; Min[dd, Length ## Map[f, {#}]], All]] &,
{list1, list2}],
{f1, 0, 10}, {f2, 0, 10}, {dd, 0, 25, 1}]

Try it with {dd, 0, 25, 1}. This both allows it to parse correctly (closing brace) and keeps it real, so to speak, by preventing dd from being real valued.

Related

Is there a way to check the value of previous value in iteration? dart

In the below code ,variable modelMap returns a map and I am iterating through that
var modelMap = parsedJson.values;
modelMap.forEach((element) {
modelName = element["ModelName"];
modelList.add(modelName) ;
});
Here I have to check if the previous iteration's element has a particular value and do my operations based on that..
for example if the pervious iteration has brandName equal to my current iteration's brandname then I will add the current model to the list.. if they are not equal I have to skip...Is it possible to do that?
You can just use some variable to save the previous. And compare to that. Here is an example where you don't take it if it is the same brandName:
List result = [];
final list = [
{'brandName': 1},
{'brandName': 2},
{'brandName': 2},
{'brandName': 3},
{'brandName': 3},
{'brandName': 3},
{'brandName': 5},
{'brandName': 3},
{'brandName': 1},
{'brandName': 1},
{'brandName': 6}
];
dynamic previous;
list.forEach((element) {
if (previous?['brandName'] != element['brandName']) {
result.add(element);
}
previous = element;
});
print(result);
//[{brandName: 1}, {brandName: 2}, {brandName: 3}, {brandName: 5}, {brandName: 3}, {brandName: 1}, {brandName: 6}]

How to cluster a list of multiple sets using clustering data-mining algorithms?

I have a list of multiple sets as an input, for example:
[
{{0, 1, 3}, {2, 4, 5, 8}, {6, 7, 9, 10}},
{{0, 1, 2, 3}, {4, 5, 8}, {6, 7, 9}, {10}},
{{0, 1, 2, 3}, {4, 5, 8}, {6, 7, 9}, {10}},
{{0}, {1, 2, 3}, {4, 5, 6, 7, 8, 9, 10}},
...
]
Every row in the list is a set, which contains multiple sets that aggregate the numbers 1~10.
I want to cluster these rows so that the rows that cluster numbers 1-10 following a similar pattern will be clustered.
I have been contemplating for a long time, still can't come up with any ideas of how to make these rows clusterable by clustering algorithm like k-means.
Please give me hints, thank you very much.

How to generate combinations of r elements in a given array of size n in swift?

I want total permutation pairs and its count
Like.
If input array is {1, 2, 3, 4} and r is 2.
then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.
Actually the total permutation pairs are
[[1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3]]
There's no need to reinvent the wheel. Apple provides a collection of useful and optimized algorithms.
Add the package Swift Algorithms to your project
Then write
import Algorithms
let array = [1, 2, 3, 4]
let permutations = array.permutations(ofCount: 2)
print(Array(permutations), permutations.count)

Elixir/Phoenix date from weekday + weeknumber

Is it possible to get date (day, month, year) by providing week number and weekday? Basically I need query for this week's monday.
I've checked Erlang, there is :erlang.date |> :calendar.day_of_the_week which returns the weekday. However, is there a way to reverse this function? Provide weekday, week number and year to get the exact date?
Thank you
Basically I need query for this week's monday.
You can compare the date's day_of_the_week against the day_of_the_week of Monday (1) and subtract that many days from the date:
def monday_in_the_week_of(date) do
from_monday = :calendar.day_of_the_week(date) - 1
date
|> :calendar.date_to_gregorian_days
|> Kernel.-(from_monday)
|> :calendar.gregorian_days_to_date
end
Demo:
defmodule A do
def monday_in_the_week_of(date) do
from_monday = :calendar.day_of_the_week(date) - 1
date
|> :calendar.date_to_gregorian_days
|> Kernel.-(from_monday)
|> :calendar.gregorian_days_to_date
end
end
for d <- 1..15 do
date = {2017, 2, d}
IO.inspect {date, A.monday_in_the_week_of(date)}
end
Output:
{{2017, 2, 1}, {2017, 1, 30}}
{{2017, 2, 2}, {2017, 1, 30}}
{{2017, 2, 3}, {2017, 1, 30}}
{{2017, 2, 4}, {2017, 1, 30}}
{{2017, 2, 5}, {2017, 1, 30}}
{{2017, 2, 6}, {2017, 2, 6}}
{{2017, 2, 7}, {2017, 2, 6}}
{{2017, 2, 8}, {2017, 2, 6}}
{{2017, 2, 9}, {2017, 2, 6}}
{{2017, 2, 10}, {2017, 2, 6}}
{{2017, 2, 11}, {2017, 2, 6}}
{{2017, 2, 12}, {2017, 2, 6}}
{{2017, 2, 13}, {2017, 2, 13}}
{{2017, 2, 14}, {2017, 2, 13}}
{{2017, 2, 15}, {2017, 2, 13}}
Is it possible to get date (day, month, year) by providing week number and weekday?
Maybe something like this would work:
#doc """
iex>get_date(1, 1, 2018)
{:ok, ~D[2018-01-01]}
"""
def get_date(weekday, _week_number, _year)
when weekday < 1 or weekday > 7,
do: {:error, "invalid weekday: #{inspect(weekday)}"}
def get_date(_weekday, week_number, _year)
when week_number < 0 or week_number > 51,
do: {:error, "invalid week_number: #{inspect(week_number)}"}
def get_date(weekday, week_number, year) do
case Date.new(year, 1, 1) do
{:ok, first_day_of_year} ->
Date.add(first_day_of_year, week_number * 7 + weekday - 1)
{:error, error} ->
{:error, error}
end
end

Esqueleto `selectDistinct` not working

selectDistinct seems to not be working for me, it's probably a simple error.
the query:
info <- runDB $
E.selectDistinct $
E.from $ \(tp `E.InnerJoin` rnd `E.InnerJoin` h) -> do
E.on (rnd E.^. RoundId E.==. h E.^. HoleRound)
E.on (tp E.^. TpartTournament E.==. rnd E.^. RoundTourn)
E.where_ ((tp E.^. TpartTournament E.==. E.val tId ))
E.orderBy [E.asc (tp E.^. TpartId)]
return (tp, rnd, h)
I'm quite sure that this represents the sql query which works:
SELECT DISTINCT tpart.id, round.name, hole.hole_num, hole.score
from tpart
inner join round on round.tourn = tpart.tournament
inner join hole on hole.round = round.id
where tpart.tournament = 1;
To view the results I have a test handler to just print the result table. Notice that for tpart 1, round 1, there are multiple hole 1 and hole 2. In postgresql SELECT DISTINICT removed these duplicates.
TpartId, RoundName, holeNum, HoleScore
Key {unKey = PersistInt64 1}, round 1, 1, 6
Key {unKey = PersistInt64 1}, round 1, 2, 4
Key {unKey = PersistInt64 1}, round 1, 1, 6
Key {unKey = PersistInt64 1}, round 1, 2, 4
Key {unKey = PersistInt64 1}, round 1, 1, 6
Key {unKey = PersistInt64 1}, round 1, 2, 4
Key {unKey = PersistInt64 1}, round 2, 1, 3
Key {unKey = PersistInt64 1}, round 2, 2, 5
Key {unKey = PersistInt64 1}, round 2, 1, 3
Key {unKey = PersistInt64 1}, round 2, 2, 5
Key {unKey = PersistInt64 1}, round 2, 1, 3
Key {unKey = PersistInt64 1}, round 2, 2, 5
Key {unKey = PersistInt64 3}, round 1, 1, 6
Key {unKey = PersistInt64 3}, round 1, 2, 4
Key {unKey = PersistInt64 3}, round 1, 1, 6
Key {unKey = PersistInt64 3}, round 1, 2, 4
Key {unKey = PersistInt64 3}, round 1, 1, 6
Key {unKey = PersistInt64 3}, round 1, 2, 4
Key {unKey = PersistInt64 3}, round 2, 1, 3
Key {unKey = PersistInt64 3}, round 2, 2, 5
Key {unKey = PersistInt64 3}, round 2, 1, 3
Key {unKey = PersistInt64 3}, round 2, 2, 5
Key {unKey = PersistInt64 3}, round 2, 1, 3
Key {unKey = PersistInt64 3}, round 2, 2, 5
Sorry for the illegibility. Any help would be appreciated!
The error was that for a certain Hole, the hole's round AND the Hole's part must equal they're respective parts. Also, the inner join was redundant in that situation.