Doing a loop in mathematica with sneg package - loops

I got a problem getting a loop done in Mathematica while using the SNEG package (it's for calculus with fermionic operators).
Here's a little bit of my code:
natord = {{cjdckd, 1, 2}, {cjdckd, 1, 3}, {cjdckd, 1, 4}, {cjdck, 1,
1}, {cjdck, 1, 2}, {cjdck, 1, 3}, {cjdck, 1, 4},
{cjdckd, 2, 3}, {cjdckd, 2, 4}, {cjdck, 2, 1}, {cjdck, 2,
2}, {cjdck, 2, 3}, {cjdck, 2, 4}, {cjdckd, 3, 4}, {cjdck, 3,
1}, {cjdck, 3, 2}, {cjdck, 3, 3}, {cjdck, 3, 4}, {cjdck, 4,
1}, {cjdck, 4, 2}, {cjdck, 4, 3}, {cjdck, 4, 4}, {cjck, 1,
2}, {cjck, 1, 3}, {cjck, 1, 4}, {cjck, 2, 3}, {cjck, 2, 4}, {cjck,
3, 4}};
Do[
j = natord[[ind, 2]] // ToString;
k = natord[[ind, 3]] // ToString;
cjd = c[0, j];
cj = c[1, j];
ckd = c[0, k];
ck = c[1, k];
cjck = nc[cj, ck];
cjdck = nc[cjd, ck];
cjdckd = nc[cjd, ckd];
korr = natord[[ind, 1]];
After this the following code generates a list of coefficients and one single value.
The list should be appended to a List M, so I have matrix in the end and the single value is going to be in a list so I get a vector.
When I'm not doing a Do-Loop and just saying
j="1";
k="2";
and
korr=cjck;
it somehow this will do it.
As you can see in the natord list, I got 28 cases which I don't want to put in each explicitly but creating a Loop doing this for me.
Hope you can help me. If you need more code tell me.
Cheers

Your code sample doesn't reveal any problem you may be encountering with Append or AppendTo.
m = {};
Do[AppendTo[m, natord[[i]]], {i, Length[natord]}]
m == natord
True

Related

Given a 2D array, find out if a given node is surrounded

Given a 2D array where each digit represents a different color, I want to be able to find out if a given node in the array is surrounded completely by one color. For example, in the 2d array below, I want to be able to confirm that the node at [3][3] is completely surrounded by the color represented by "1". Is there an existing common algorithm that accomplishes this?
{{1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 0, 0},
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 2, 2, 2, 2, 1, 1, 0},
{1, 1, 1, 2, 2, 1, 1, 1},
{1, 1, 1, 2, 2, 2, 2, 1},
{1, 1, 1, 1, 1, 2, 1, 1},
{1, 1, 1, 1, 1, 1, 2, 1}}
EDIT: Sorry I am not asking about whether the target node is immediately surrounded. I am asking about whether if you move out from the target node, you can reach the edge of the array without crossing over the boundary color.
My current code is the following, but it's not quite working
let squaresChecked = []
let squareSurrounded = true
let boardSize = 15
let gameBoard = new Array(boardSize)
for(let i=0; i<gameBoard.length; i++){
gameBoard[i] = Array(boardSize).fill('white')
}
checkSurrounded(x, y, boundaryColor){
if(x >= boardSize || y >= boardSize || x < 0 || y < 0){
squareSurrounded = false
return
}
if(gameBoard[x][y] === boundaryColor){return}
if(squaresChecked.includes(x + ' ' + y)){return}
squaresChecked.push(x + ' ' + y)
checkSurrounded(x+1, y, boundaryColor)
checkSurrounded(x-1, y, boundaryColor)
checkSurrounded(x, y+1, boundaryColor)
checkSurrounded(x, y-1, boundaryColor)
}
This should check for top, left, right, bottom for given cell at row,col in 2D array if surrounded by var surroundedBy (1).
row = 2
col = 2
arr = [[1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 1, 1, 0],
[1, 1, 1, 2, 2, 1, 1, 1],
[1, 1, 1, 2, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 2, 1, 1],
[1, 1, 1, 1, 1, 1, 2, 1]]
surroundedBy = 1
if(arr[row-1][col] ===surroundedBy && arr[row+1][col] === surroundedBy && arr[row][col - 1] === surroundedBy && arr[row][col + 1] === surroundedBy) {
console.log('surrounded');
}else{
console.log('not surrounded');
}

Identify rows of one numpy array that gave rise to another numpy array

I think I'm missing something obvious. Consider the following code:
import numpy as np
a = np.array([[ 0, 2, 0, 5, 4, 6, 2, 4],
[ 3, 4, 0, 1, 0, 7, 4, 6],
[ 2, 6, 3, 5, 2, 5, 5, 8],
[ 0, 1, 0, 8, 0, 5, 8, 10],
[ 7, 9, 2, 7, 0, 6, 7, 2],
[ 0, 1, 4, 9, 0, 7, 9, 9],
[ 0, 6, 7, 5, 6, 2, 4, 13],
[ 0, 1, 1, 4, 1, 3, 2, 3]]
# isolate columns 2,3,6,7
mask = [False,False, True, True,False,False, True, True]
b = a[:,mask]
# determine rows of b having unique elements
s = np.sort(b, axis=1)
c = b[~(s[:,:-1] == s[:,1:]).any(1)]
c looks like:
c = [[ 0, 5, 2, 4],
[ 0, 1, 4, 6],
[ 7, 5, 4, 13],
[ 1, 4, 2, 3]]
QUESTION: How do I 'recover' the rows of a that gave rise to the rows of c?
The output should be like:
d = [[ 0, 2, 0, 5, 4, 6, 2, 4],
[ 3, 4, 0, 1, 0, 7, 4, 6],
[ 0, 6, 7, 5, 6, 2, 4, 13],
[ 0, 1, 1, 4, 1, 3, 2, 3]]

How to get depth of tree of arrays given total num items and max array size?

Given this divide algorithm and sample data:
const data = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0
]
function divide(data, size) {
const result = []
for (let i = 0; i < data.length; i += size) {
const chunk = data.slice(i, i + size);
result.push(chunk)
}
if (result.length > size) {
return divide(result, size)
}
return result;
}
const result = divide(data, 5);
console.log(result)
How do you calculate the number of levels in the resulting tree? In this case of block size 5, I think there are 3 are 4 levels about, but what is the equation to use so you don't have to compute the actual tree? Basically getDepth(numItems, blockSize)?
Every recursive call reduces the input size by a factor of blocksize.
Starting with your example of 100 items. This is grouped into 20, these 20 are grouped into 4 and the algorithm ends.
The expression that capture this is log with a base of blocksize.
f(n,m) = ceil(log_base_m(n))
The depth of the example tree is
        ceil(log5(99))
In general:
        ceil(logchunksize(datasize-1))

Merge arrays inside of arrays

I have this array:
a = [[1,2,3,4,5],[3,5,6,8,12,45],[3,2,1,5,7,9,10,11],[3,5,6,8,2,1,3,4,6]]
I want to merge its inner arrays so that they become:
a = [[1,2,3,4,5,3,5,6,8,12,45],[3,2,1,5,7,9,10,11,3,5,6,8,2,1,3,4,6]]
How can I do this?
You need to do
a = [
[1, 2, 3, 4, 5],
[3, 5, 6, 8, 12, 45],
[3, 2, 1, 5, 7, 9, 10, 11],
[3, 5, 6, 8, 2, 1, 3, 4, 6]
]
a.each_slice(2).map(&:flatten)
# => [
# [1, 2, 3, 4, 5, 3, 5, 6, 8, 12, 45],
# [3, 2, 1, 5, 7, 9, 10, 11, 3, 5, 6, 8, 2, 1, 3, 4, 6]
# ]
Read the method each_slice(n)
Iterates the given block for each slice of n elements. If no block is given, returns an enumerator.

Can TogglerBar be used as multiple CheckBox in Mathematica?

Would it be possible to have a TogglerBar instead of the 2 Check Box to show or not the different Shapes.
With Green & Red written in each Button of the TogglerBar ?
Manipulate[
Graphics[{If[thePink, {Pink, Disk[{5, 5}, 3]}],
If[theGreen, {Green, Disk[{15, 2}, 1]}]},
PlotRange -> {{0, 20}, {0, 10}}], {{thePink, True,
Style["Pink", Black, Bold, 12]}, {True, False}}, {{theGreen, True,
Style["Green", Black, Bold, 12]}, {True, False}}]
The actual Manipulate object I am trying to adjust can be found there : http://www.laeh500.com/LAEH/COG.html
The purpose being to replace the CheckBox by a nice TogglerBar.
Like this?
Manipulate[
Graphics[{
{White, Circle[{5, 5}, r]}, (* For Mma 7 compatibility*)
If[MemberQ[color, "Red"], {Pink, Disk[{5, 5}, r]}],
If[MemberQ[color, "Green"], {Green, Disk[{4, 2}, r]}]},
PlotRange -> {{0, 20}, {0, 10}}],
{{r, 1, "Radius"}, 1, 5, 1, ControlType -> Setter},
{{color, "Red", "Color"}, {"Red", "Green"}, ControlType -> TogglerBar},
LabelStyle -> Large]
Edit
Answering your comment, I think your notebook could benefit from a template like this one:
Manipulate[
Graphics[
{
{White, Circle[{5, 5}, r]},(* For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "Circle"], {Red, Circle [{5, 5}, r]}],
If[MemberQ[whatToDisplay, "Square"], {Blue, Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"], {Black, Line [Tuples[{3, 4}, 2]]}],
},
PlotRange -> {{0, 20}, {0, 10}}
],
(* Controls follow *)
{{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1, ControlType -> Slider
, ControlPlacement-> Top
},
Control#{{whatToDisplay, True, Style["What", Black, Bold, 12]},
{"Circle", "Square", "Other"},
ControlType -> TogglerBar,
Appearance -> "Vertical",
ControlPlacement -> Left
}
]
How about this?
Manipulate[
Show[Graphics[myObject],
PlotRange -> {{0, 20}, {0, 10}}], {{myObject, {},""}, {{Pink,
Disk[{5, 5}, 3]} ->
Style["Pink", Black, Bold, 12], {Green, Disk[{15, 2}, 1]} ->
Style["Green", Black, Bold, 12]}}, ControlType -> TogglerBar]
How about
Manipulate[
Graphics[{#} & /# x,
PlotRange -> {{0, 20}, {0, 10}}],
{{x, {}, "Colour"},
{{Pink, Disk[{5, 5}, 3]} \[Rule] "Pink",
{Green, Disk[{15, 2}, 1]} \[Rule] "Green"},
ControlType -> TogglerBar}]
it's ugly and inelegant, though! Dynamic manipulation is not my favourite use of Mathematica, so this is sort of trial and error for me too...
EDIT: Slightly less ugly now...
EDIT2: Added a label

Resources