Page 1 of 1

Novice question about 2D arrays

Posted: Wed Jan 28, 2009 1:21 pm
by tasos
Hi all,

I have a question regarding the values stored in a 2D array.
More specifically, I create an array full of zeros using

Code: Select all

for ($i=0; $i<48; $i++) {
    for ($j=0; $j<32; $j++) {
    $Ba[$i][$j] = 0;}}
then for specific cells I add values using

Code: Select all

 
$Ba[0][0]= 1;   
$Ba[1][0]= 3.7494e-033; 
...
$Ba[43][29]= 0.47468;
Then I try to make the following calculation (array multiplication) but for some reason I don't understand why $t is always zero. I read somewhere that floatval cannot by used with arrays so I tried the multiplication without the floatval function but still the same error.

I would be grateful, if you could give me some help on this.

Cheers

Code: Select all

    $row=0; 
    while ($row <48)
    {
        $t=0;
        for ($col=0; $col<32; $col++) 
               {        
        $t = $t + (floatval($Ba[row][col])*floatval($h1[col]));     
        }
        //echo $t;  // This is always zero
        $ra[row] = $t;
        $row=$row + 1;
    }

Re: Novice question about 2D arrays

Posted: Wed Jan 28, 2009 2:17 pm
by andyhoneycutt
It looks like you're missing the '$' on your variables for $row and $col:

Code: Select all

 
    $row=0; 
    while ($row < 48)
    {
        $t=0;
        for ($col=0; $col<32; $col++) 
        {        
//[color=#FF0000]            $t = $t + (floatval($Ba[row][col])*floatval($h1[col]));     [/color] // should be:
            $t = $t + (floatval($Ba[$row][$col]) * floatval($h1[$col]));
        }
        //echo $t;  // This is always zero
        $ra[$row] = $t;
        $row=$row + 1;
    }
 
Try fixing that and let me know if it works or fails.

-Andy

Re: Novice question about 2D arrays

Posted: Wed Jan 28, 2009 2:28 pm
by tasos
Hi Andy,

Thanks for your prompt reply. The missing $ was an error occurred when pasting the code into the forum.
Even with the $ the result is still zero.

I have tried an echo $Ba[1][1] just before the multiplication but I get nothing as an output. All the code is in the same function.

Cheers

Re: Novice question about 2D arrays

Posted: Wed Jan 28, 2009 3:17 pm
by nor0101
I think this is what you're trying to do:

Code: Select all

 
<?php
 
    function identityMatrix ($width, $height) {
        $result;
        for ($i=0; $i<$height; $i++) {
            for ($j=0; $j<$width; $j++) {
                $result[$i][$j] = 0;
            }
        }
        return $result;
    }
 
    function randomScalarMatrix($height) {
        $result;
        for ($i=0; $i<$height; $i++) {
            $result[$i] = rand()/5555555;
        }
        return $result;
    }
 
    /*
    *   @pre:   $a1 is a 2-D non-jagged array
    *           i.e. count($a1[0]) == count($a1[1]) == ... == count($a1[n])
    *
    *           $a2 is a 1-D array s.t. count($a2) == count($a1[0])
    *
    *   @post:  return a $result array with same dimensions as $a1,
    *           with values $result[$i][$j] == $a1[$i][$j] * $a2[$j]
    */
    function array_mult($a1, $a2) {
        $row_len = count($a1);
        $col_len = count($a1[0]);
        $result;
        for ($row=0; $row<$row_len; $row++) {
            for ($col=0; $col<$col_len; $col++) {
                $result[$row][$col] = $a1[$row][$col] * $a2[$col];
            }
        }
        return $result;
    }
 
    // generate initial matrices
 
    $x = identityMatrix(32, 48);
    $y = randomScalarMatrix(32);
 
    // set some values in $x
    
    $x[0][0] = 1;
    $x[1][0] = 3.7494e-33;
    $x[5][8] = 3.14159;
 
    $z = array_mult($x, $y);
    print_r($z);
?>
 

Re: Novice question about 2D arrays

Posted: Wed Jan 28, 2009 3:27 pm
by nor0101
Sorry, I didn't address the typecasting part of your question. There is implicit casting that takes place in situations like this. For example, when performing an arithmetic operation with an integer value and a float value as operands, the integer is automatically converted to a float so that the operation can take place. There is a possible loss of precision here for very large ints (over a billion or so), as only the leading 9 (if i remember correctly) sig bits are moved to the mantissa to make room for the exponent part. In general, explicit typecasting of primitives is unnecessary unless you are trying to override this default behavior.

If you're looking to be really accurate look into the BC Math library here: http://us3.php.net/bc

Re: Novice question about 2D arrays

Posted: Thu Jan 29, 2009 2:06 am
by tasos
Hi, nor0101

Thanks you very much for your complete and highly helpful replies.
I will try the code you propose right away.

Cheers