Mathematica simplification and acceleration - wolfram-mathematica
I have been trying to integrate a function AND get an output that is sufficiently simple as to be usable. Simply using the Simplify and FullSimplify commands has not done nearly enough, and it takes me over 2 hours to get a result from this integration. Are there any further tricks I can use to force further simplifications? Any assumptions I can put in to make it simpler or faster?
Be warned, the example output is stupidly long, so much so that it is impossible to read, much less manually simplify.
EDIT: Updated input and output to reflect help from commentators. Still nowhere near being usable.
Input (ai, bi, ci,and di are unit vectors, and so will be replaced with the appropriate cosine later. ki has a value but is still kept track of so the proper cosine can be used later. the cosines are independent of the integration, so it makes it marginally shorter to do it this way):
r = {r1, r2, r3};
a = {a1, a2, a3}/\[Sigma]a;
b = {b1, b2, b3}/\[Sigma]b;
c = {c1, c2, c3}/\[Sigma]c;
d = {d1, d2, d3}/\[Sigma]d;
k = {k1, k2, k3};
S = {{S11, S12, S13}, {S21, S22, S23}, {S31, S32, S33}};
FullSimplify[Integrate[(1/(2*Pi*\[Sigma]a*\[Sigma]b))*Exp[-(1/2)*((a.r)^2 + (b.r)^2)]*(1/(2*Pi*\[Sigma]c*\[Sigma]d))*Exp[-(1/2)*((c.r)^2 + (d.r)^2)], {r1, -Infinity, Infinity}, {r2, -Infinity, Infinity}, {r3, -Infinity, Infinity}, Assumptions -> Element[{r1, r2, r3, a1, a2, a3, b1, b2, b3, c1, c2, c3, d1, d2, d3, k1, k2, k3, S11, S12, S13, S21, S22, S23, S31, S32, S33, \[Sigma]a, \[Sigma]b, \[Sigma]c, \[Sigma]d, \[Tau]}, Reals] && \[Sigma]a > 0 && \[Sigma]b > 0 && \[Sigma]c > 0 && \[Sigma]d > 0 && \[Tau] >= 0 && 1 >= a1 >= -1 && 1 >= a2 >= -1 && 1 >= a3 >= -1 && 1 >= b1 >= -1 && 1 >= b2 >= -1 && 1 >= b3 >= -1 && 1 >= c1 >= -1 && 1 >= c2 >= -1 && 1 >= c3 >= -1 && 1 >= d1 >= -1 && 1 >= d2 >= -1 && 1 >= d3 >= -1]]]]
Output:
ConditionalExpression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]a^2 \[Sigma]b^2 + (b2 b3 \[Sigma]a^2 +
a2 a3 \[Sigma]b^2) \[Sigma]d^2) +
c2^2 (d3^2 \[Sigma]a^2 \[Sigma]b^2 + (b3^2 \[Sigma]a^2 +
a3^2 \[Sigma]b^2) \[Sigma]d^2) + \[Sigma]c^2 ((a3 d2 -
a2 d3)^2 \[Sigma]b^2 +
b3^2 (d2^2 \[Sigma]a^2 + a2^2 \[Sigma]d^2) -
2 b2 b3 (d2 d3 \[Sigma]a^2 + a2 a3 \[Sigma]d^2) +
b2^2 (d3^2 \[Sigma]a^2 + a3^2 \[Sigma]d^2)))]] < \[Pi]]
Related
Shuffle elements from three vectors using AVX
After doing few operations I got following three intermediate vectors. __m256 Vec1 = [a0 a1 a2 a3 a4 a5 a6 a7]; //8 float values __m256 Vec2 = [b0 b1 b2 b3 b4 b5 b6 b7]; //8 float values __m256 Vec3 = [c0 c1 c2 c3 c4 c5 c6 c7]; //8 float values I should rearrange these vectors as shown below for further processing. __m256 ReVec1 = [a0 a1 b0 b1 c0 c1 a2 a3]; __m256 ReVec2 = [b2 b3 c2 c3 a4 a5 b4 b5]; __m256 ReVec3 = [c4 c5 a6 a7 b6 b7 c6 c7]; How can I shuffle three Vectors in AVX?
Trouble with a complicated recursive algorithm
I am having trouble getting the logic right for a method I'm trying to write and I thought it might be a fun problem for someone else to have a look at. The target language is Java, though I'm just going to present it generically so as not to discourage anyone from sharing their thoughts. The input to the method is an arbitrary number of hierarchical sets of data and the output is a list of combinations of members of that data where each combination has one member from each set of hierarchical data. However, every possible combination isn't included. The key to building the combinations is whether or not a given element is a leaf member in the hierarchy or not and each element of each set knows its name and whether or not it is a leaf. First, I'll give an example and then I'll try to articulate the rules. For concreteness, we'll consider that the input is the following three sets: A1 B1 C1 A10 B10 C10 A11 B100 C11 B101 C12 B102 C13 B103 B104 B105 B11 B110 B111 B112 B113 B114 The corresponding output would look like this: (1) A1 B1 C1 (37) A10 B111 C11 (73) A11 B103 C10 (2) A10 B1 C1 (38) A10 B111 C12 (74) A11 B103 C11 (3) A10 B10 C1 (39) A10 B111 C13 (75) A11 B103 C12 (4) A10 B100 C1 (40) A10 B112 C1 (76) A11 B103 C13 (5) A10 B100 C10 (41) A10 B112 C10 (77) A11 B104 C1 (6) A10 B100 C11 (42) A10 B112 C11 (78) A11 B104 C10 (7) A10 B100 C12 (43) A10 B112 C12 (79) A11 B104 C11 (8) A10 B100 C13 (44) A10 B112 C13 (80) A11 B104 C12 (9) A10 B101 C1 (45) A10 B113 C1 (81) A11 B104 C13 (10) A10 B101 C10 (46) A10 B113 C10 (82) A11 B11 C1 (11) A10 B101 C11 (47) A10 B113 C11 (83) A11 B110 C1 (12) A10 B101 C12 (48) A10 B113 C12 (84) A11 B110 C10 (13) A10 B101 C13 (49) A10 B113 C13 (85) A11 B110 C11 (14) A10 B102 C1 (50) A10 B114 C1 (86) A11 B110 C12 (15) A10 B102 C10 (51) A10 B114 C10 (87) A11 B110 C13 (16) A10 B102 C11 (52) A10 B114 C11 (88) A11 B111 C1 (17) A10 B102 C12 (53) A10 B114 C12 (89) A11 B111 C10 (18) A10 B102 C13 (54) A10 B114 C13 (90) A11 B111 C11 (19) A10 B103 C1 (55) A11 B1 C1 (91) A11 B111 C12 (20) A10 B103 C10 (56) A11 B10 C1 (92) A11 B111 C13 (21) A10 B103 C11 (57) A11 B100 C1 (93) A11 B112 C1 (22) A10 B103 C12 (58) A11 B100 C10 (94) A11 B112 C10 (23) A10 B103 C13 (59) A11 B100 C11 (95) A11 B112 C11 (24) A10 B104 C1 (60) A11 B100 C12 (96) A11 B112 C12 (25) A10 B104 C10 (61) A11 B100 C13 (97) A11 B112 C13 (26) A10 B104 C11 (62) A11 B101 C1 (98) A11 B113 C1 (27) A10 B104 C12 (63) A11 B101 C10 (99) A11 B113 C10 (28) A10 B104 C13 (64) A11 B101 C11 (100) A11 B113 C11 (29) A10 B11 C1 (65) A11 B101 C12 (101) A11 B113 C12 (30) A10 B110 C1 (66) A11 B101 C13 (102) A11 B113 C13 (31) A10 B110 C10 (67) A11 B102 C1 (103) A11 B114 C1 (32) A10 B110 C11 (68) A11 B102 C10 (104) A11 B114 C10 (33) A10 B110 C12 (69) A11 B102 C11 (105) A11 B114 C11 (34) A10 B110 C13 (70) A11 B102 C12 (106) A11 B114 C12 (35) A10 B111 C1 (71) A11 B102 C13 (107) A11 B114 C13 (36) A10 B111 C10 (72) A11 B103 C1 Though I understand the pattern unambiguously, it's hard for me to articulate the rules for the same reason that it's hard for me to write the method. Basically, if an elemnet is a non leaf, it's only used to build combinations with the root element from the sets to it's right. If instead an element is a leaf, it's crossed with all of the posisble combinations to it's right. Obviously the problem is recursive and I feel like the psuedo code should look something like this: buildCombinations(element, setNumber, numberOfSets) { if (element.IsLeaf( )) { List list = buildCombinations(elementFromNextList, setNumber++, numberOfSets) CrossJon element with list } else { IfOnlyIKnew! } } But I've been staring at it for two days and I just can't quite wrap my head around it. I supsect that I've not done an adaquate job of explaining the situation so please feel free to ask questions for clarification. In any event, this strikes me as the kind of problem that might be obvious to someone a bit smarter than me and I'd love it if anyone can offer any suggestions. Thanks in advance!
You forgot to include B105 in your output. That will add 10 additional lines to the output - A10 B105 C1 A10 B105 C10 A10 B105 C11 A10 B105 C12 A10 B105 C13 A11 B105 C1 A11 B105 C10 A11 B105 C11 A11 B105 C12 A11 B105 C13 That mistake aside, this problem is a small difference from cartesian product for which there are many well-known algorithms. The key difference is that a leaf node will never appear after a tree node. One naive algorithm would generate the cartesian product of all tree nodes and then remove the ones where leafs appear after trees. But we can do better. Instead of wastefully visiting paths that will be pruned from the output, we can intelligently skip ahead when a tree node is encountered - JavaScript Here's a quick implementation using JavaScript - const traverse = ([ t, ...more ], r = []) => // base case: empty t t === undefined ? [ r ] // inductive: t.leaf : t.leaf ? traverse(more, [...r, t.value]) // inductive: t.tree : [ [ ...r, t.value, ...more.map(t => t.value) ] // <-- , ...t.children.flatMap(c => traverse([ c, ...more ], r)) ] The input to our traverse function is an array of leaf or tree nodes - const leaf = (value) => ({ leaf, value }) const tree = (value, ...children) => ({ tree, value, children }) Given the trees in your original post - A1 B1 C1 A10 B10 C10 A11 B100 C11 B101 C12 B102 C13 B103 B104 B105 B11 B110 B111 B112 B113 B114 We can represent them using tree and leaf like so - const treeA = tree("A1", leaf("A10"), leaf("A11")) const treeB = tree ( "B1" , tree("B10", leaf("B100"), leaf("B101"), leaf("B102"), leaf("B103"), leaf("B104"), leaf("B105")) , tree("B11", leaf("B110"), leaf("B111"), leaf("B112"), leaf("B113"), leaf("B114")) ) const treeC = tree("C1", leaf("C10"), leaf("C11"), leaf("C12"), leaf("C13")) const result = traverse([ treeA, treeB, treeC ]) // [ [ "A1 B1 C1" ], [ ... ], ... ] .map(path => path.join(" ")) // [ "A1 B1 C1", "A10 B1 C1", ... ] console.log(result.join("\n")) // "A1 B1 C1\nA10 B1 C1\n..." console.log(result.length) // 117 Expand the snippet below to verify the result in your browser - const leaf = (value) => ({ leaf, value }) const tree = (value, ...children) => ({ tree, value, children }) const traverse = ([ t, ...more ], r = []) => // base case: empty t t === undefined ? [ r ] // inductive: t.leaf : t.leaf ? traverse(more, [...r, t.value]) // inductive: t.tree : [ [ ...r, t.value, ...more.map(t => t.value) ] , ...t.children.flatMap(c => traverse([ c, ...more ], r)) ] const treeA = tree("A1", leaf("A10"), leaf("A11")) const treeB = tree ( "B1" , tree("B10", leaf("B100"), leaf("B101"), leaf("B102"), leaf("B103"), leaf("B104"), leaf("B105")) , tree("B11", leaf("B110"), leaf("B111"), leaf("B112"), leaf("B113"), leaf("B114")) ) const treeC = tree("C1", leaf("C10"), leaf("C11"), leaf("C12"), leaf("C13")) const result = traverse([ treeA, treeB, treeC ]) .map(path => path.join(" ")) console.log(result.join("\n")) console.log(result.length) Output - A1 B1 C1 A10 B1 C1 A10 B10 C1 A10 B100 C1 A10 B100 C10 A10 B100 C11 A10 B100 C12 A10 B100 C13 A10 B101 C1 A10 B101 C10 A10 B101 C11 A10 B101 C12 A10 B101 C13 A10 B102 C1 A10 B102 C10 A10 B102 C11 A10 B102 C12 A10 B102 C13 A10 B103 C1 A10 B103 C10 A10 B103 C11 A10 B103 C12 A10 B103 C13 A10 B104 C1 A10 B104 C10 A10 B104 C11 A10 B104 C12 A10 B104 C13 A10 B105 C1 A10 B105 C10 A10 B105 C11 A10 B105 C12 A10 B105 C13 A10 B11 C1 A10 B110 C1 A10 B110 C10 A10 B110 C11 A10 B110 C12 A10 B110 C13 A10 B111 C1 A10 B111 C10 A10 B111 C11 A10 B111 C12 A10 B111 C13 A10 B112 C1 A10 B112 C10 A10 B112 C11 A10 B112 C12 A10 B112 C13 A10 B113 C1 A10 B113 C10 A10 B113 C11 A10 B113 C12 A10 B113 C13 A10 B114 C1 A10 B114 C10 A10 B114 C11 A10 B114 C12 A10 B114 C13 A11 B1 C1 A11 B10 C1 A11 B100 C1 A11 B100 C10 A11 B100 C11 A11 B100 C12 A11 B100 C13 A11 B101 C1 A11 B101 C10 A11 B101 C11 A11 B101 C12 A11 B101 C13 A11 B102 C1 A11 B102 C10 A11 B102 C11 A11 B102 C12 A11 B102 C13 A11 B103 C1 A11 B103 C10 A11 B103 C11 A11 B103 C12 A11 B103 C13 A11 B104 C1 A11 B104 C10 A11 B104 C11 A11 B104 C12 A11 B104 C13 A11 B105 C1 A11 B105 C10 A11 B105 C11 A11 B105 C12 A11 B105 C13 A11 B11 C1 A11 B110 C1 A11 B110 C10 A11 B110 C11 A11 B110 C12 A11 B110 C13 A11 B111 C1 A11 B111 C10 A11 B111 C11 A11 B111 C12 A11 B111 C13 A11 B112 C1 A11 B112 C10 A11 B112 C11 A11 B112 C12 A11 B112 C13 A11 B113 C1 A11 B113 C10 A11 B113 C11 A11 B113 C12 A11 B113 C13 A11 B114 C1 A11 B114 C10 A11 B114 C11 A11 B114 C12 A11 B114 C13 117 // results.length from JavaScript to Java I don't know Java but I can help rewrite the program above in a way that might be easier to translate - JavaScript Java ================================================================ Array.prototype.concat List.AddAll, Stream.concat Array.prototype.map Stream.map Array.prototype.flatMap Stream.flatMap Array.prototype.join String.join (c => ___) Lambda (c -> ___) [ foo, ...bar ] (no equivalent, see below) const traverse = (trees = []) => // call traverseHelper with trees and initial path for all nodes, [] traverseHelper(trees, []) function traverseHelper ([ t, ...more ], r = []) { // when no t if (t === undefined) return [ r ] // when t.leaf else if (t.leaf) return traverseHelper(more, [ ...r, t.value ]) // when t.tree else { // when t.tree is encountered, do not add t.value to the children paths // simply copy the .value of each tree to the path for this node const path = r.concat([ t.value ]).concat(more.map(t => t.value)) // return the path for this node, combined wit the results for // the children nodes. return [ path ] .concat(t.children.flatMap(c => traverseHelper([ c, ...more ], r))) } } To my knowledge, Java does not support spread syntax. These are just syntactic sugar for common array manipulations - function foo(x, y, ...z) { console.log("foo x:", String(x)) console.log("foo y:", String(y)) console.log("foo z:", String(z)) } function bar([ x, ...more ]) { console.log("bar x:", String(x)) console.log("bar more:", String(more)) } const a = [1,2,3] const b = 4 const c = [5,6] foo(...a, b, ...c) // <-- foo(1,2,3,4,5,6) // x: 1 // y: 2 // z: 3,4,5,6 const d = [...a, b, ...c] // <-- [1,2,3,4,5,6] bar(d) // x: 1 // more: 2,3,4,5,6 racket/scheme Racket is a great language for discussing and sharing algorithms because there is almost no syntax and programs are made up of simple parts - #lang racket (define (traverse ts (r null)) (cond ;; base case: empty ((null? ts) (list r)) ;; inductive: leaf ((null? (cdar ts)) (traverse (cdr ts) (cons (caar ts) r))) ;; inductive: tree (else (cons (append (reverse (map car ts)) r) (append-map (lambda (c) (traverse (cons c (cdr ts)) r)) (cdar ts)))))) This is almost a direct translation of the JavaScript code with the exception that path results are constructed in reverse order. (define a '(a1 (a10) (a11))) (define b '(b1 (b10 (b100) (b101) (b102) (b103) (b104) (b105)) (b11 (b110) (b111) (b112) (b113) (b114)))) (define c '(c1 (c10) (c11) (c12) (c13))) (define result (map reverse (traverse (list a b c)))) result (map reverse ...) is used to display the results using the correct ordering - '((a1 b1 c1) (a10 b1 c1) (a10 b10 c1) (a10 b100 c1) (a10 b100 c10) (a10 b100 c11) (a10 b100 c12) (a10 b100 c13) (a10 b101 c1) (a10 b101 c10) (a10 b101 c11) (a10 b101 c12) (a10 b101 c13) (a10 b102 c1) (a10 b102 c10) (a10 b102 c11) (a10 b102 c12) (a10 b102 c13) (a10 b103 c1) (a10 b103 c10) (a10 b103 c11) (a10 b103 c12) (a10 b103 c13) (a10 b104 c1) (a10 b104 c10) (a10 b104 c11) (a10 b104 c12) (a10 b104 c13) (a10 b105 c1) (a10 b105 c10) (a10 b105 c11) (a10 b105 c12) (a10 b105 c13) (a10 b11 c1) (a10 b110 c1) (a10 b110 c10) (a10 b110 c11) (a10 b110 c12) (a10 b110 c13) (a10 b111 c1) (a10 b111 c10) (a10 b111 c11) (a10 b111 c12) (a10 b111 c13) (a10 b112 c1) (a10 b112 c10) (a10 b112 c11) (a10 b112 c12) (a10 b112 c13) (a10 b113 c1) (a10 b113 c10) (a10 b113 c11) (a10 b113 c12) (a10 b113 c13) (a10 b114 c1) (a10 b114 c10) (a10 b114 c11) (a10 b114 c12) (a10 b114 c13) (a11 b1 c1) (a11 b10 c1) (a11 b100 c1) (a11 b100 c10) (a11 b100 c11) (a11 b100 c12) (a11 b100 c13) (a11 b101 c1) (a11 b101 c10) (a11 b101 c11) (a11 b101 c12) (a11 b101 c13) (a11 b102 c1) (a11 b102 c10) (a11 b102 c11) (a11 b102 c12) (a11 b102 c13) (a11 b103 c1) (a11 b103 c10) (a11 b103 c11) (a11 b103 c12) (a11 b103 c13) (a11 b104 c1) (a11 b104 c10) (a11 b104 c11) (a11 b104 c12) (a11 b104 c13) (a11 b105 c1) (a11 b105 c10) (a11 b105 c11) (a11 b105 c12) (a11 b105 c13) (a11 b11 c1) (a11 b110 c1) (a11 b110 c10) (a11 b110 c11) (a11 b110 c12) (a11 b110 c13) (a11 b111 c1) (a11 b111 c10) (a11 b111 c11) (a11 b111 c12) (a11 b111 c13) (a11 b112 c1) (a11 b112 c10) (a11 b112 c11) (a11 b112 c12) (a11 b112 c13) (a11 b113 c1) (a11 b113 c10) (a11 b113 c11) (a11 b113 c12) (a11 b113 c13) (a11 b114 c1) (a11 b114 c10) (a11 b114 c11) (a11 b114 c12) (a11 b114 c13))
Ruby Program to print pattern [closed]
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 3 years ago. Improve this question I have a hash and print that in the following Pattern : Input 1 : { a: [1,2,3,4,5,6,7,8,9], b: [1,2,3,4,5,6], c: [2,3,4,5,6,7] } Output 1 : A1 A2 A3 A4 A5 A6 A7 A8 A9 B1 B2 B3 B4 B5 B6 C2 C3 C4 C5 C6 C7 Input 2 : { a: [1,2,3,4,5,6,7], b: [2,3,4,5,6], c: [1,2,3,4,5,6,7] } Output 2 : A1 A2 A3 A4 A5 A6 A7 B2 B3 B4 B5 B6 C1 C2 C3 C4 C5 C6 C7 For e.g. If we consider Input 1 then the expectation is, the solution should add blank spaces at the missing number position. => It should return : [ ["A1","A2","A3","A4","A5","A6","A7","A8","A9"], ["B1","B2","B3","B4","B5","B6","","",""], ["","C2","C3","C4","C5","C6","C7","",""] ]
You can use collection, map or each for this data = {a: [1,2,3,4,5,6,7,8,9],b: [1,2,3,4,5,6],c: [2,3,4,5,6,7]} data.map{|k,v| (1..9).map{|a| data[k].include?(a) ? k.to_s.upcase() +a.to_s : ' '}}
MPI Gathering columns into bigger matrix
Lets say I 3 different matrix on 3 different threads: a1 a2 a3 a4 a5 a6 b1 b2 b3 b4 b5 b6 c1 c2 c3 c4 c5 c6 d1 d2 d3 d4 d5 d6 e1 e2 e3 e4 e5 e6 f1 f2 f3 f4 f5 f6 I want to gather them by using MPI_Gather into this one matrix a1 a2 a3 a4 a5 a6 b1 b2 b3 b4 b5 b6 c1 c2 c3 c4 c5 c6 d1 d2 d3 d4 d5 d6 e1 e2 e3 e4 e5 e6 f1 f2 f3 f4 f5 f6 I am using MPI_Gather(&oldMatrix,N/size,columnType,&newMatrix,N/size,gatherColType,0,MPI_COMM_WORLD); Where coltype is: MPI_Type_vector(N, 1, N/size, MPI_FLOAT, &column); MPI_Type_commit(&column); MPI_Type_create_resized(column, 0, 1*sizeof(float), &columnType); MPI_Type_commit(&columnType); However, I am not sure how should i create gatherColType (if it is needed). Can you help me about that? note: N is matrix size and size is thread count (i.e. 3 in this question)
How nested 'for' loops are inserting newlines in this program automatically?
#include <stdio.h> int main() { int alpha,numeric; for(alpha='A';alpha<'K';alpha++) { for(numeric=0;numeric<10;numeric++) { printf("%c%d\t",alpha,numeric); } } return 0; } The output I got is as follows. A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 G0 G1 G2 G3 G4 G5 G6 G7 G8 G9 H0 H1 H2 H3 H4 H5 H6 H7 H8 H9 I0 I1 I2 I3 I4 I5 I6 I7 I8 I9 J0 J1 J2 J3 J4 J5 J6 J7 J8 J9 But, more neat and in form of a matrix, without any space between lines. Someone please tell me how the printing is entering to new line even though there is no newline(\n) character in the program.