I have this gauss_eliminate function, but instead of dealing with real numbers, I want it to work on binary values.
I need the GF2 gauss_eliminate function, where the input is binary and the output is binary.
This produces real values, not binary, eg
0.57142857142857
0.71428571428571
-0.42857142857143
-0.28571428571429
0.14285714285714
Gaussian elimination has these 3 allowed steps:
1) Swapping two rows (for achieving a certain look)
2) Multiplying a row by a nonzero number,
3) Adding a multiple of one row to another row.
-- in GF2: addition operation is XOR : 0+0=0, 0+1=1, 1+0=1, 1+1=0 --and--
multiplication is AND operation: 0*0=0,0*1=0,1*0=0,1*1=1
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
$tmp = $A[$i][$col] / $A[$col][$col];
for ($j = $col + 1; $j < $N; $j++) $A[$i][$j] -= $tmp * $A[$col][$j];
$A[$i][$col] = 0;
$b[$i] -= $tmp * $b[$col];
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) $tmp -= $x[$j] * $A[$col][$j];
$x[$col] = $tmp / $A[$col][$col];
}
return $x;
}
new code #1, still doesn't work:
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
for ($j = $col + 1; $j < $N; $j++)
$A[$i][$j]=( $A[$i][$j] != $A[$col][$j] ) ? 1 : 0;
$A[$i][$col] = 0;
$b[$i]=( $b[$i] != $b[$col] ) ? 1 : 0;
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
# $tmp = $b[$col];
# for ($j = $N - 1; $j > $col; $j--) $tmp -= $x[$j] * $A[$col][$j];
$x[$col] = ( $x[$col] != $A[$col][$j] ) ? 1 : 0;
}
return $x;
}
New code #2 - still doesn't work - tmp setup to alternate
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) { $j = $i; $max = $tmp; }
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
# $tmp = $A[$i][$col] / $A[$col][$col];
for ($j = $col + 1; $j < $N; $j++) $A[$i][$j]=($A[$i][$j] != $A[$col][$j] ? 1 : 0);
$A[$i][$col] = 0;
$b[$i] = ( $b[$i] != $b[$col] ? 1 : 0);
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) $tmp = 1 - $tmp;
$x[$col] = ($tmp != $A[$col][$j] ? 1 : 0);
}
return $x;
}
It appears I found the right syntax. I am getting the right result for one example, after modifying the code in a way that makes sense.... converting - to +, and this + to XOR, while / is ignored and * is AND.
Still it would be nice to get a confirmation that this code is correct.
function gauss_eliminate($A, $b, $N) {
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
# $tmp = $A[$i][$col] / $A[$col][$col];
# for ($j = $col + 1; $j < $N; $j++) {
# $A[$i][$j] -= $tmp * $A[$col][$j];
# }
# $A[$i][$col] = 0;
# $b[$i] -= $tmp * $b[$col];
$tmp = $A[$i][$col];
for ($j = $col + 1; $j < $N; $j++) {
# $A[$i][$j] = $A[$i][$j] + ( $tmp * $A[$col][$j] );
$A[$i][$j] = ( $A[$i][$j] != ( $tmp && $A[$col][$j] ) ) ? 1 : 0;
}
$A[$i][$col] = 0;
# $b[$i] = $b[$i] + ($tmp * $b[$col]);
$b[$i] = ( $b[$i] != ($tmp && $b[$col]) ) ? 1 : 0;
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) {
# $tmp -= $x[$j] * $A[$col][$j];
# $tmp = $tmp + ($x[$j] * $A[$col][$j]);
$tmp = ( $tmp != ($x[$j] && $A[$col][$j]) ) ? 1 : 0;
}
# $x[$col] = $tmp / $A[$col][$col];
$x[$col] = $tmp;
}
return $x;
}
The commented text is the old (non GF2 code) as well as my "middle step" of showing where I convert + to XOR, * to AND, etc
Related
Using the following code I can get the DCT of an image in PHP. Then I need to convert this back in to the compressed image. How can I achieve that?
<?php
$results = array();
$image1 = "baboon.jpg";
$ima = ImageCreateFromJPEG($image1);
$N1 = imagesx($ima);
$N2 = imagesy($ima);
$rows = array();
$row = array();
for ($j = 0; $j < $N2; $j++) {
for ($i = 0; $i < $N1; $i++)
$row[$i] = imagecolorat($ima, $i, $j);
$rows[$j] = dct1D($row);
}
for ($i = 0; $i < $N1; $i++) {
for ($j = 0; $j < $N2; $j++)
$col[$j] = $rows[$j][$i];
$results[$i] = dct1D($col);
}
print_r($results);
function dct1D($in) {
$results = array();
$N = count($in);
for ($k = 0; $k < $N; $k++) {
$sum = 0;
for ($n = 0; $n < $N; $n++) {
$sum += $in[$n] * cos($k * pi() * ($n + 0.5) / ($N));
}
$sum *= sqrt(2 / $N);
if ($k == 0) {
$sum *= 1 / sqrt(2);
}
$results[$k] = $sum;
}
return $results;
}
?>
Also I need to know how can I add some extra details like another message to this image too.. (image steganography). Please help. Thanks
Why the following code do not return me 0 => 'Zero' for the first line but 0 => 0 ?
for ($i = 0; $i <= 30; $i += 1) {
if($i == 0) { $array[$i] = 'Zero'; }
$array[$i] = $i;
}
for ($i = 30; $i <= 100; $i += 5) {
$array[$i] = $i;
}
for ($i = 100; $i <= 200; $i += 10) {
$array[$i] = $i;
}
return $array;
Thanks.
You set element 0 to Zero and then overwrite it in the next line with 0.
if($i == 0) { $array[$i] = 'Zero'; }
$array[$i] = $i;
You probably want an else...
if($i == 0) {
$array[$i] = 'Zero'; }
else {
$array[$i] = $i;
}
for ($i = 0; $i <= 30; $i += 1)
{
if($i == 0)
{
$array[$i] = 'Zero';
}
$array[$i] = $i;
}
should be:
for ($i = 0; $i <= 30; $i += 1)
{
if($i == 0)
{
$array[$i] = 'Zero';
}
else
{
$array[$i] = $i;
}
}
There is Fatal Error in this code:
Call-time pass-by-reference has been removed on line 108. this is
Boyer Moore Algorithm
function suffixes($pattern, &$suffixes)
{
$m = strlen($pattern);
$suffixes[$m - 1] = $m;
$g = $m - 1;
for ($i = $m - 2; $i >= 0; --$i) {
if ($i > $g && $suffixes[$i + $m - 1 - $f] < $i - $g) {
$suffixes[$i] = $suffixes[$i + $m - 1 - $f];
} else {
if ($i < $g) {
$g = $i;
}
$f = $i;
while ($g >= 0 && $pattern[$g] == $pattern[$g + $m - 1 - $f]) {
$g--;
}
$suffixes[$i] = $f - $g;
}
}
}
function badCharacters
function badCharacters($pattern, &$badChars)
{
$m = strlen($pattern);
for ($i = 0; $i < $m - 1; ++$i) {
$badChars[$pattern{$i}] = $m - $i - 1;
}
}
function goodSuffixes
function goodSuffixes($pattern, &$goodSuffixes)
{
$m = strlen($pattern);
$suff = array();
suffixes($pattern, $suff);
for ($i = 0; $i < $m; $i++) {
$goodSuffixes[$i] = $m;
}
for ($i = $m - 1; $i >= 0; $i--) {
if ($suff[$i] == $i + 1) {
for ($j = 0; $j < $m - $i - 1; $j++) {
if ($goodSuffixes[$j] == $m) {
$goodSuffixes[$j] = $m - $i - 1;
}
}
}
}
for ($i = 0; $i < $m - 2; $i++) {
$goodSuffixes[$m - 1 - $suff[$i]] = $m - $i - 1;
}
}
function boyer_moore
function boyer_moore($pattern, $text)
{
$n = strlen($text);
$m = strlen($pattern);
$goodSuffixes = array();
$badCharacters = array();
//this is the line 108
goodSuffixes($pattern, &$goodSuffixes);
badCharacters($pattern, &$badCharacters);
$j = 0;
while ($j < $n - $m) {
for ($i = $m - 1; $i >= 0 && $pattern[$i] == $text[$i + $j]; $i--);
if ($i < 0) {
echo $j;
$j += $goodSuffixes[0];
} else {
$j += max($goodSuffixes[$i], $badCharacters[$text[$i + $j]] - $m + $i + 1);
}
}
}
boyer_moore($pattern, $text);
What is causing this error?
It's clear from the error message:
goodSuffixes($pattern, &$goodSuffixes);
badCharacters($pattern, &$badCharacters);
The '&' is the character that is attempting to pass by reference at calltime. Remove the '&' from in front of the 2 variable names.
goodSuffixes($pattern, $goodSuffixes);
badCharacters($pattern, $badCharacters);
Here I need an array of all the numbers generated by permutation and also here i am giving n and k from a html form.
function combination_number($k,$n){
$n = intval($n);
$k = intval($k);
if ($k > $n){
return 0;
} elseif ($n == $k) {
return 1;
} else {
if ($k >= $n - $k){
$l = $k+1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $n-$k ; $i++)
$m *= $i;
} else {
$l = ($n-$k) + 1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $k ; $i++)
$m *= $i;
}
}
return $l/$m;
}
function array_combination($le, $set){
$lk = combination_number($le, count($set));
$ret = array_fill(0, $lk, array_fill(0, $le, '') );
$temp = array();
for ($i = 0 ; $i < $le ; $i++)
$temp[$i] = $i;
$ret[0] = $temp;
for ($i = 1 ; $i < $lk ; $i++){
if ($temp[$le-1] != count($set)-1){
$temp[$le-1]++;
} else {
$od = -1;
for ($j = $le-2 ; $j >= 0 ; $j--)
if ($temp[$j]+1 != $temp[$j+1]){
$od = $j;
break;
}
if ($od == -1)
break;
$temp[$od]++;
for ($j = $od+1 ; $j < $le ; $j++)
$temp[$j] = $temp[$od]+$j-$od;
}
$ret[$i] = $temp;
}
for ($i = 0 ; $i < $lk ; $i++)
for ($j = 0 ; $j < $le ; $j++)
$ret[$i][$j] = $set[$ret[$i][$j]];
return $ret;
}
$number = $_REQUEST['number'];
for($i=0;$i<$number;$i++)
{
$arr[$i]=$i+1;
}
$k = $_REQUEST['select'];
$permutations = array_combination($k, $arr);
echo "<pre>";
print_r($permutations);
This code is working but its not giving all the numbers generated by Permutation. I need those numbers also. Please tell me how can i get those? Please help.
I'm not sure if it is the best solution, but this seems to do what you want
function array_combination($le, $set){
// Your existing code
// …
for ($i = 0 ; $i < $lk ; $i++)
for ($j = 0 ; $j < $le ; $j++)
$ret[$i][$j] = $set[$ret[$i][$j]];
// Changes start here
$desiredArray = $ret;
foreach($ret as $innerArray)
{
$desiredArray[] = array_reverse($innerArray);
}
return $desiredArray;
}
Logfiles are full of this error:
Undefined offset: 255 in /var/www/html/site2/functions.inc.php on line 764"
The line 764 is the following:
$counter[$i] = $counter[$x];
The whole function see below.
Can somebody help me to fix it? Thanks.
function Encode($data,$pwd) {
$pwd_length = strlen($pwd);
for ($i = 0; $i < 255; $i++) {
$key[$i] = ord(substr($pwd, ($i % $pwd_length)+1, 1));
$counter[$i] = $i;
}
$x = '';
for ($i = 0; $i < 255; $i++) {
$x = ($x + $counter[$i] + $key[$i]) % 256;
$temp_swap = $counter[$i];
$counter[$i] = $counter[$x];
$counter[$x] = $temp_swap;
}
$a = '';
$j = '';
$Zcrypt = '';
for ($i = 0; $i < strlen($data); $i++) {
$a = ($a + 1) % 256;
$j = ($j + $counter[$a]) % 256;
$temp = $counter[$a];
$counter[$a] = $counter[$j];
$counter[$j] = $temp;
$k = $counter[(($counter[$a] + $counter[$j]) % 256)];
$Zcipher = ord(substr($data, $i, 1)) ^ $k;
$Zcrypt .= chr($Zcipher);
}
return $Zcrypt;
}
Your for-loop stops when $i >= 255. You don't want that. I think you want the for-loop to stop at $i>255.
So change
for ($i = 0; $i < 255; $i++) {
into
for ($i = 0; $i <= 255; $i++) {
And you're good to go!