I want to do numerical integration in Mathematica over a large dataset like: {{x1,y1(c)}, {x2,y2(c)}, {x3,y3(c)}, {x4,y4(c)}..} where y1=(a1+c)/b1, y2= (a2+c)/b2, y3=(a3+c)/b3, y4=(a4+c)/b4.... and a1, a2, a3, a4, b1, b2, b3, b4..... are just numbers and c is a constant. After the integration, I want to plot the resulting function as a function of c. How can I do that?
I have a dot graph, and I am using the constraint=false because for some of the edges, I don't want the edge to affect the rank of the nodes. Unfortunately, it appears that this means that the dot engine doesn't use that edge for layout of nodes within a rank, and it also seems to do a worse job at routing the edge itself. For example, see this graph:
digraph G {
subgraph G1 {
a1 -> b1
d1 -> b1
b1 -> c1
a1 -> c1 [constraint=false]
}
subgraph G2 {
a2 -> b2
d2 -> b2
b2 -> c2
a2 -> c2
}
}
see online
the a1 -> c1 edge could be routed left of the b1 node, but isn't. I don't want a1 -> c1 to be used for computing rank.
Is there a way to get the "best of both worlds" here? I.e. a way to get the better layout (like the a2 -> c2 edge) but not use the edge for computing rank?
I don't know a way to not use a edge for ranking, but use it for the layout (without affecting the ranking). Edges with constraint=false seem to be laid out after the placement of the nodes is determined.
What's left are some "hacks", not sure whether they are applicable in a generic manner for all of your use cases.
For example, if you make sure that the nodes linked with such an edge are mentioned before the others, the resulting layout is - at least in this example - improved:
subgraph G1 {
d1, a1;
a1 -> b1
d1 -> b1
b1 -> c1
a1 -> c1 [constraint=false]
}
example
I have an intermediate pig structure like
(A, B, (n. no Cs))
example:
(a1,b1, (c11,c12))
(a2,b2, (c21))
(a3,b3, (c31,c32, c33))
Now, I want the data in format
(a1, b1, c11)
(a1, b2, c12)
(a2, b2, c21) etc.
How do I go about doing it?
Essentially I want the size of the tuples, and then use this size for running a nested for loop.
Can you try the below approach?
input
a1 b1 (c11,c12)
a2 b2 (c21)
a3 b3 (c31,c32,c33)
PigScript:
A = LOAD 'input' AS(f1,f2,T:(f3:chararray));
B = FOREACH A GENERATE f1,f2,FLATTEN(T);
C = FOREACH B GENERATE f1,f2,FLATTEN(TOKENIZE(T::f3));
DUMP C;
Output:
(a1,b1,c11)
(a1,b1,c12)
(a2,b2,c21)
(a3,b3,c31)
(a3,b3,c32)
(a3,b3,c33)
square([A1, A2, A3|_], [B1, B2, B3|_], [C1, C2, C3|_]):-
all_diff([A1, A2, A3, B1, B2, B3, C1, C2, C3]).
squares([[]|_]):- !.
squares([[A1,A2,A3|A4], [B1,B2,B3|B4], [C1,C2,C3|C4], [D1,D2,D3|D4], [E1,E2,E3|E4], [F1,F2,F3|F4], [G1,G2,G3|G4], [H1,H2,H3|H4], [I1,I2,I3|I4]]):-
square([[A1,A2,A3], [B1,B2,B3], [C1,C2,C3]]),
square([[D1,D2,D3], [E1,E2,E3], [F1,F2,F3]]),
square([[G1,G2,G3], [H1,H2,H3], [I1,I2,I3]]),
squares([A4, B4, C4, D4, E4, F4, G4, H4, I4]).
I'm getting a uncaught exception: error(existence_error(procedure,square/1),squares/1) - I'm new to Prolog so I'm not quite sure what this is saying (well, where it's saying it's going wrong).
This is inside a sudoku program, by the way.
squares iterates through the 3x3 squares of the sudoku puzzle starting with the top left, middle left, and bottom left - moving to the top middle, middle middle, bottom middle, and so on.
square checks the focus square - A1, A2, A3 are the first three values of the first row
B1, B2, B3 are the first three values of the second row, and so on. It assembles them into one array and checks to make sure they are all different.
on the lines
square([[A1,A2,A3], [B1,B2,B3], [C1,C2,C3]]),
square([[D1,D2,D3], [E1,E2,E3], [F1,F2,F3]]),
square([[G1,G2,G3], [H1,H2,H3], [I1,I2,I3]]),
you'll note that the type is ([[],[],[]]), however, the function, square is ([],[],[]).
Changing the lines to
square([A1,A2,A3], [B1,B2,B3], [C1,C2,C3]),
square([D1,D2,D3], [E1,E2,E3], [F1,F2,F3]),
square([G1,G2,G3], [H1,H2,H3], [I1,I2,I3]),
fixes the problem.
Is there any special design pattern or algorithm available for this problem?
There are multiple items (A1,A2,A3 ..An) and I want to arrange them, some of them are related to the other ones and only can comes before or after them.
For example A2 can be placed only after A4 and “An” can be placed at the end of the set. But the point is that the sequence of some items can be interchangeable and based on sequence some items should not be in the set.
For examlpe consider this scenario
There are 6 items
A1, A2, A3, A4, A5, A6
And the rules are
A1 must be at the first place (always),
A2 can be after A4,
A5 can be in the set only if A3 has been there before it
A6 comes at the end of the set and it is a mandatory member but it only can be there if all other valid items have been in the set before it !
Valid sets are like this
A1, A4, A3, A2, A5, A6
A1, A4, A2, A6
Invalid sets
A4, A3, A2, A5, A6 (A1 is missed)
A1, A4, A3, A2, A5 (A6 is missed)
A1, A3, A2, A6 (A2 only comes after A4)
Note: I have to validate the input! and input can have any order! I mean I don’t want to sort the items I want to validate an input set from the user
As a sample, based on my example above the below sets are all valid
{A1, A4, A3, A2, A5, A6}
{A1, A4, A2, A3, A5, A6}
{A1, A3, A4, A2, A5, A6}
{A1, A3, A5, A4, A2, A6}
So user might enter any of these as an input and all of them are valid based on the defined conditions!
Any idea of any special design pattern or algorithm which can be applied to this problem? Number of items or the rules might get change in the future!
“BalusC” had been removed my “Design pattern” tag! But so far I think the best way to handle this problem might be the command pattern. I mean I considered each item as a command from the user and I defined a validation process for the command (“canExecute”) I am going to code it in C# and since ICommand interface in .Net has “canExecute” method I think I will use it to validate the command based on condition. (Execute method just added the item to the result set!) I have not coded it yet so I am not sure how complicated the validation process might be. I thought maybe someone has some idea how I can combine the command pattern and a validation algorithm to achieve the goals.
I might be wrong so any idea or suggestion can be helpful. Thanks.
This should be solvable with a variation of a topological sort.
Basically, you build a directed acyclic graph where there is an edge from Ai to Aj if Ai must come before Aj in the result. The topological sort will then give you a valid order for the A's.
This will not deal with the rule that some items may be missing, but that should be simple to layer on top of this.
A1, A4, A2, A6
cannot be a valid set, because
A6 comes at the end of the set and it is a mandatory member but it
only can be there if all other valid items have been in the set before
it