how to redirect GAP output to a text file on a local drive? - gap-system

Example
m1;
[ [ -1, 0, 0, 0, 0, 0 ], [ 1, 1, -1, 0, 0, 0 ], [ 0, 0, -1, 0, 0, 0 ], [ 0, 0, 0, -1, 0, 0 ], [ 0, 0, 0, 1, 1, 0 ],
[ 0, 0, 0, 0, 0, -1 ] ]
in the Windows version of the GAP system, how do it redirect any output to a text file on a local drive?

You may use LogTo command to save inputs and outputs of the whole GAP session, or you may use PrintTo to print the object to the text file.
Enter ?LogTo and `?PrintTo' in GAP to see the documentation.
P.S. If you prefer to ask questions about GAP in StackExchange framework, I'd recommend to try to ask them at Mathematics Q&A site here.

Related

Weird Behaviour when nesting 2D arrays in a 3D array

Currently I'm working on a Minimax algorithm for four-in-a-row in javascript. I have decided to store possible moves in a array, continuing to nest arrays within arrays for every branch.
However, when I attempt to edit a certain value in an array, it edits all other values within the same column.
Code:
var BOARD = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
// Put 3 x 2D Arrays into one 3D Array
var NODE_TREE = [BOARD, BOARD, BOARD];
NODE_TREE[1][2][0] = 2;
console.log(NODE_TREE);
Chrome V8 interpreter output:
[ [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 0, 0 ] ],
[ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 0, 0 ] ],
[ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 0, 0 ] ] ]
What should happen but dosen't:
[ [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ],
[ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 0, 0 ] ],
[ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ] ]
Javascript seems to ignore the First dimension index number. This issue only happens when I use an existing array nested within an array. If I were to make a 3D array from scratch, this bug does not happen.
For a lazy initalization, you could use a parsed JSON string.
This generates an independent object without references to the original object.
var BOARD = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
],
JSON_BOARD = JSON.stringify(BOARD),
NODE_TREE = [JSON.parse(JSON_BOARD), JSON.parse(JSON_BOARD), JSON.parse(JSON_BOARD)];
NODE_TREE[1][2][0] = 2;
console.log(NODE_TREE);
.as-console-wrapper { max-height: 100% !important; top: 0; }
When you put BOARD 3 times in to the array, you are not "copying" the items in to the new array, you are actually just creating 3 references to the original BOARD array.
Therefore any change made to any one of those references will be reflected in all instances of BOARD.
If you want to create 3 distinct arrays in your 3d array you will need to clone or copy the arrays. You can do this by mapping on the outer array and copy the contents with Array.prototype.slice();
var BOARD = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
];
// function to map and copy arrays with .slice()
function sliceArrays (outerArray){
return outerArray.map((arr)=>{
return arr.slice()
});
}
var NODE_TREE = [sliceArrays(BOARD), sliceArrays(BOARD), sliceArrays(BOARD)];
NODE_TREE[1][2][0] = 2;
console.log(NODE_TREE);
//[ [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ],
// [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 2, 0, 0 ] ],
// [ [ 0, 0, 0 ], [ 0, 0, 0 ], [ 0, 0, 0 ] ] ]

Handsontable - Calculating values that include commas

I have a web page that uses handsontable. In this page, I'm trying to programmatically put some data into the spreadsheet and execute some formulas. My formulas work fine until a value includes commas. For example, in this JSFiddle, I have the following code:
var hot = null;
initHot();
loadFormulas();
loadData();
function initHot() {
var container = document.getElementById('example');
var hCols = [ 'A', 'B', 'C', 'D', 'E' ];
var hRows = [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ];
var sdata = [
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0 ]
];
hot = new Handsontable(container, {
startRows: hRows.length,
rowHeaders: hRows,
startCols: hCols.length,
colHeaders: hCols,
data: sdata,
formulas: true,
height: 300
});
}
function loadFormulas() {
// In my app, this happens async, but I'm just plugging it in here to demo it.
hot.setDataAtCell(2, 0, '=A1-A2');
hot.setDataAtCell(9, 0, '=SUM(A3:A9)');
}
function loadData() {
// In my app, this happens at runtime, but I'm just plugging it in here to demo it.
hot.setDataAtCell(0, 0, '1000');
hot.setDataAtCell(1, 0, '3000');
hot.setDataAtCell(3, 0, '5000');
hot.setDataAtCell(4, 0, '6500');
hot.setDataAtCell(5, 0, '7000');
hot.setDataAtCell(6, 0, '8000');
hot.setDataAtCell(7, 0, '9000');
hot.setDataAtCell(8, 0, '10,000');
}
Cell A10 should be result in the value 43500. However, it calculates incorrectly because of the comma in cell A9. When a comma is present, Handsontable views the value in the cell as zero. If you change 10,000 to 10000, it calculates properly. How do I support commas in values such that the formulas still execute correctly?
Your Spreadsheet probably not calculating right because of regional settings of localmachine. Try to change "." with "," or change Date - Time - Number format on local machine.

Chai Expect a deep array to include not working with Chai-Immutable

AssertionError: expected 'List [ List [ 0, "a", 0, 0 ], List [ 0, 0, 0, 0 ], List [ 0, 0, 0, 0 ], List [ 0, 0, 0, 0 ] ]' to include 'a'
I use Chai and chai-immutable.
I am calling:
expect(nextState).to.deep.include("a");
Why doesn't this work?
Note that even though .include() is overridden by chai-immutable, it behaves just like Chai does on native arrays:
Consider:
expect([
[0, "a", 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
]).to.deep.include("a");
This fails with:
AssertionError: expected [ Array(4) ] to include 'a'
To solve this, I see at least 2 ways to do so (as far as I know) using Chai alone. There might also be Chai plugins to help you with this.
Using .flatten
Immutable.js Lists come with .flatten() which would flatten your matrix of Lists into a single List:
expect(new List([
new List([0, "a", 0, 0]),
new List([0, 0, 0, 0]),
new List([0, 0, 0, 0]),
new List([0, 0, 0, 0]),
]).flatten()).to.deep.include("a");
A failure would output like this:
AssertionError: expected 'List [ 0, "a", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]' to include 'z'
Using .satisfy
Chai has a .satisfy() assertion that allows for more manual control:
expect(new List([
new List([0, "a", 0, 0]),
new List([0, 0, 0, 0]),
new List([0, 0, 0, 0]),
new List([0, 0, 0, 0]),
])).to.satisfy(matrix => matrix.some(row => row.includes("a")));
This avoids flattening the value being tested against, but because chai-immutable does not override .satisfy(), the output in case of failure is not too pretty:
AssertionError: expected { Object (size, _origin, ...) } to satisfy [Function]

javascript board game piece placement

I'm writing a board game in java script, and what i'm trying to accomplish is: layout the board(chess/checkers format) Then add pieces to the board based on position. So for example i want to be able to write code for piece a to be moved onto tile 10.
So far in my code i have a loop to create the board but don't a method to properly name the tiles, so that the piece can correctly be placed on the tile.
for (i=0; i<64; i++){
var tile = cc.Sprite.create(res.myTile_png);
this.addChild(tile,0);
x = centerpos.x + ((i % 8) - 3.5) * tile.getBoundingBox().width;
y = centerpos.y + (Math.floor(i / 8) - 3.5) * tile.getBoundingBox().height;
tile.setPosition(x,y);
}
One way to go about doing this would be to assign a unique integer identifier to every distinct piece in the game, and then maintain a matrix of dimensions equal to the # of rows x # of columns on the board, with values of the piece identifiers in the correct address in the matrix that would correspond to their position on the board.
For instance, the starting arrangement of pieces in checkers can be represented by:
[
[ 0, -1, 0, -1, 0, -1, 0, -1 ],
[ -1, 0, -1, 0, -1, 0, -1, 0 ],
[ 0, -1, 0, -1, 0, -1, 0, -1 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 1, 0, 1, 0, 1, 0, 1, 0 ],
[ 0, 1, 0, 1, 0, 1, 0, 1 ],
[ 1, 0, 1, 0, 1, 0, 1, 0 ]
]
with, say, -1 representing white, and 1 representing red pieces on the board.
The tile elements of the board can also be kept in a matrix, so that the two matrices can be iterated over together to place the pieces in corresponding locations.
The unique ids can also then be used as CSS class names, or image file names to be attached to the element representing the piece.

vb.net integer division

I'm trying to convert this ASP.net(vb) script to pure Javascript and I'm running into this problem. The script I'm converting has the following:
Dim F as Integer
Dim Theta(100) as Double
F ends up with the value: 1
Theta ends up with the following Value:
[0, 1.2669233316773538, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Then some math calculations are done and inside the formula the Theta array is used like so:
Cos(Theta(F / 2))
Doing simple math here, F / 2, (or 1 / 2) = 0.5 There is no array key 0.5 so in my Javascript code its like saying:
Math.cos(Theta[0.5])
Which ends up doing:
Math.cos(undefined)
Which of course breaks the math.
My question is simple. I do not have an IIS server or any way to make modifications to the vb.net script so that I can add debugging output to see how its working. I'm basically doing this blind and with very little knowledge of any .net languages nor ASP.
Since F is an Integer, is dividing F in the vb.net script going to yield a whole number? Is the rounding forced due to the type of F?
Like, in this script, which one of these is happening?: Theta[0] (concatenated) or Theta[1] (rounded) or Theta[0.5] (same as javascript)?
Help is greatly appreciated!
Have a look at this answer: VB.NET vs C# integer division
VB.NET has two operator for the division:
"/" division: the result will be rounded
"\" Integer division: the result will be truncated
In your case as your script uses "/", the result will be 1 (1 / 2 = 0.5 rounded to 1).
Your JavaScript
Math.cos(Theta[0.5])
should become
Math.cos(Theta[Math.round(0.5)])

Resources