Hello everyone,<br><br>I am writing some functions that will be made into a class, for doing basic serial operations on cells (i.e. arrays of floats). The operations I have so far are: transpose, reverse, invert, permute, shuffle.
<br><br>The next function to finish is multiply, which does Boulez-style cell multiplication. For the function to work correctly, it should remove any duplicates from the array. Unfortunately, my brain's a little rusty right now. I'm sure I did this in the only computer class I ever took (C++). But I'm too sleepy to figure out the correct solution offhand, I think the ways I'm coming up with are inefficient. Can anyone help?
<br><br>I've gotten really spoiled, by usually coding these sorts of things in Python, where its amazingly quick to do list manipulations of any kind.<br><br>Here's my code so far, which works except multiply is a dummy function:
<br><br>//cellFun.ck<br><br>public class Argh<br>{<br> fun static void print(float cell[])<br> {<br> for (0 => int i; i<cell.cap(); i++)<br> {<br> <<< cell[i] >>> ;<br> }<br> }<br>
<br> fun static void remove (float a[], int index)<br> {<br> a.cap()-1 => int newLength;<br> float temp[newLength];<br> for (0 => int i; i < newLength; i++)<br> {<br> if (i<index)<br> {
<br> a[i] => temp[i];<br> }<br> else<br> {<br> a[i+1] => temp[i];<br> }<br> }<br> float a[newLength];<br> for (0 => int i; i < newLength; i++ )<br> {<br> temp[i] => a[i];
<br> }<br> }<br><br> //match<br> fun static int match(float cell[], float element)<br> {<br> cell.cap() => int length;<br> -1 => int position;<br> for (0 => int i; i<length; i++)<br> {<br> if (cell[i] == element)
<br> {<br> i => position;<br> break;<br> }<br> }<br> return position;<br> }<br><br>}<br><br>fun float[] copyCell(float cell[])<br>{<br> cell.cap() => int length;<br> float newCell[length];
<br> for (0 => int i; i<length; i++)<br> {<br> cell[i] => newCell[i];<br> }<br> return (newCell);<br>}<br><br><br>//transpose<br>fun float[] tranCell(float cell[], float amount)<br>{<br> cell.cap() => int length;
<br> float newCell[length];<br> for (0 => int i; i<length; i++)<br> {<br> cell[i] + amount => newCell[i];<br> }<br> return(newCell);<br>}<br><br>//reverse<br>fun float[] revCell(float cell[])<br>{<br> cell.cap
() => int length;<br> float newCell[length];<br> length - 1 => int counter;<br> for (0 => int i; i<length; i++)<br> {<br> cell[i]=> newCell[counter];<br> counter--;<br> }<br> return(newCell);<br>
}<br><br>//invert()<br><br>fun float[] invCell(float cell[])<br>{<br> cell.cap() => int length;<br> float newCell[length];<br> cell[0] => newCell[0];<br> for (1 => int i; i<length; i++)<br> {<br> (2 * cell[0]) - (cell[i]) => newCell[i];
<br> }<br> return(newCell);<br>}<br><br><br>//shufCell(): <br><br>fun float[] shufCell(float cell[])<br>{<br> cell.cap() => int length;<br> copyCell(cell) @=> float newCell[];<br> //--- Shuffle elements by randomly exchanging each with one other.
<br> for (0 => int i; i<length; i++)<br> {<br> std.rand2(0,length-1) => int r; // generate a random position<br> newCell[i] => float temp; <br> newCell[r] => newCell[i]; <br> temp => newCell[r];
<br> }<br> return(newCell);<br>}<br><br><br>//permCell(float cell[], int amount):<br><br>fun float[] permCell (float cell[], int amount)<br>{<br> cell.cap() => int length;<br> float newCell[length];<br> for (0=>int i; i<length; i++)
<br> {<br> cell[(amount+i)%length] => newCell[i];<br> }<br> return(newCell);<br>}<br><br>//multiply(float cell[], float multCell[])<br>//unfinished<br>//look for a match<br>//store the position in a new array<br>
<br>fun float[] multCell (float cell[], float mult[])<br>{<br> <<<"unfinished">>>;<br> return [0.0];<br>}<br><br>//tests!!!<br><br>fun void testShiz()<br>{<br> [0.0,2.0,4.0,6.0,8.0,2.0,10.0] @=> float testCell[];
<br> copyCell(testCell) @=> float theCopy[];<br> [0.0,1.0,2.0] @=> float multi[];<br> <br> <<<"print cell">>>;<br> Argh.print(testCell);<br> <br> <<<"match (return position of first occurrance of
2.0 in cell)">>>;<br> <<<Argh.match(testCell,2.0)>>>;<br> <br> <<<"transpose cell by 2.0 (returns a copy)">>>;<br> Argh.print(tranCell(testCell, 2.0));<br>
<br> <<<"reverse cell (returns a copy)">>>;<br> Argh.print(revCell(testCell));<br> <br> <<<"invert cell (returns a copy)">>>;<br> Argh.print(invCell(testCell));
<br> <br> <<<"shuffle cell (returns a copy)">>>;<br> Argh.print(shufCell(testCell));<br> <br> <<<"permCell(permute by 2, returns a copy)">>>;<br> Argh.print(permCell(testCell, 2));
<br> <br> <<<"multCell (multiply by [0.0,1.0,2.0], returns a copy">>>;<br> Argh.print(multCell(testCell,multi));<br> <br> <<<"print the original">>>;<br> Argh.print
(testCell);<br> <br> <<<"permute the original by 2 and print">>>;<br> permCell(testCell, 2) @=> testCell;<br> Argh.print(testCell);<br> <br> <<<"print the copy">>>;
<br> Argh.print(theCopy);<br>}<br><br>testShiz();<br><br>