Related
Self-Aware Arrays.
An integer array int[] A is self-aware if for each i < A.length, A[i] is the exact number of occurrences of i in A.
For example, [2, 0, 2, 0] is self-aware.
Write an algorithm accepts a positive integer n as input, when executed, prints out a list of all self-aware arrays of that length.
Here are some Self-aware arrays have been found. But I haven't found an algorithm.
N = 4
{2, 0, 2, 0}
{1, 2, 1, 0}
N = 5
{2, 1, 2, 0, 0}
N = 6
None
N = 7
{3, 2, 1, 1, 0, 0, 0}
N = 8
{4, 2, 1, 0, 1, 0, 0, 0}
N = 9
{5, 2, 1, 0, 0, 1, 0, 0, 0}
N = 10
{6, 2, 1, 0, 0, 0, 1, 0, 0, 0}
N = 11
{7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0}
N = a
{ a-4, 2, 1, <a-7 0s>, 1, 0, 0, 0 }
Seems we have the properties
sum(A[i]) = n = sum(i * A[i])
A[0] = sum( (i-1) * A[i] ) i>=2
Outer loop: Generate all candidates of length n. This can be done with any convenient set product software; you need n elements in the range 0:n-1. For each candidate, filter and check (below).
Filter:
The sum of the candidates elements must equal n. Note that if you're writing your own candidate generator, you can use this to greatly reduce the quantity of candidates.
The first element can never be 0.
Check:
Count the quantity of each number 0:n-1 in order. The resulting list is your check count. If this list is equal to the original candidate, you have a solution: print it.
Shortcuts:
Are you allowed to use known results? It's long proved that for n >= 7, the formula you give is the only solution: you can directly generate the one solution and return. For n < 7, brute force will produce all valid solutions quite quickly.
function valueCountMap(arr) {
let obj = {};
for (let i = 0; i < arr.length; i++) {
let temp = arr[i];
let val = obj[temp];
if (val) {
obj[temp] += 1;
} else {
obj[temp] = 1;
}
}
return obj;
}
function isSelfAware(arr) {
let map = valueCountMap(arr);
for (const key in map) {
if (map.hasOwnProperty(key)) {
const found = map[key];
if (arr[key] !== found) {
return false;
}
}
}
return true;
}
let arr1 = [7, 2, 1, 0, 0, 0, 0, 1, 0, 0, 0];
console.log(isSelfAware(arr1));
I am currently trying to vectorize the following piece of code:
velocity[0] = 0.0;
velocity[1] = 0.0;
velocity[2] = 0.0;
for (int i = 0; i < PARAMQ; i++)
{
velocity[0] += currentCell[i] * LATTICEVELOCITIES[i][0];
velocity[1] += currentCell[i] * LATTICEVELOCITIES[i][1];
velocity[2] += currentCell[i] * LATTICEVELOCITIES[i][2];
}
where LATTICEVELOCITIES is an 2d integer array
static const int32_t LATTICEVELOCITIES[PARAMQ][3] = {{0, -1, -1},
{-1, 0, -1},
{0, 0, -1},
{1, 0, -1},
{0, 1, -1},
{-1, -1, 0},
{0, -1, 0},
{1, -1, 0},
{-1, 0, 0},
{0, 0, 0},
{1, 0, 0},
{-1, 1, 0},
{0, 1, 0},
{1, 1, 0},
{0, -1, 1},
{-1, 0, 1},
{0, 0, 1},
{1, 0, 1},
{0, 1, 1}
};
and currentCell is an array of doubles.
Unfortunately, all the examples I could find only deal with arrays of the same type and I don't know how to load only two integers into one 128bit register and then convert them to doubles.
Thanks for your help in advance.
Firstly, as per the comments above, I'm going to assume that it's OK to transpose LATTICEVELOCITIES:
static const int32_t LATTICEVELOCITIES[3][PARAMQ] = {
{ 0, -1, 0, 1, 0, -1, 0, 1, -1, 0, 1, -1, 0, 1, 0, -1, 0, 1, 0 },
{ -1, 0, 0, 0, 1, -1, -1, -1, 0, 0, 0, 1, 1, 1, -1, 0, 0, 0, 1 },
{ -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1 }
};
Now let's iterate through the data, processing two elements per iteration, and have a single scalar iteration at the end to take care of the last (odd) element:
__m128d v0, v2, v2;
v0 = v1 = v2 = _mm_setzero_pd();
for (int i = 0; i < PARAMQ - 1; i += 2)
{
__m128d vc, vl0, vl1, vl2;
__m128i vtemp;
vc = _mm_loadu_pd(¤tCell[i]);
vtemp = _mm_loadu_si128((__m128i *)&LATTICEVELOCITIES[0][i]);
vl0 = _mm_cvtepi32_pd(vtemp);
vtemp = _mm_loadu_si128((__m128i *)&LATTICEVELOCITIES[1][i]);
vl1 = _mm_cvtepi32_pd(vtemp);
vtemp = _mm_loadu_si128((__m128i *)&LATTICEVELOCITIES[2][i]);
vl2 = _mm_cvtepi32_pd(vtemp);
v0 = _mm_add_pd(v0, _mm_mul_pd(vc, vl0));
v1 = _mm_add_pd(v1, _mm_mul_pd(vc, vl1));
v2 = _mm_add_pd(v2, _mm_mul_pd(vc, vl2));
}
v0 = _mm_hadd_pd(v0, v0);
v1 = _mm_hadd_pd(v1, v1);
v2 = _mm_hadd_pd(v2, v2);
_mm_store_sd(&velocity[0], v0);
_mm_store_sd(&velocity[1], v1);
_mm_store_sd(&velocity[2], v2);
if (i < PARAMQ)
{
velocity[0] += currentCell[i] * LATTICEVELOCITIES[0][i];
velocity[1] += currentCell[i] * LATTICEVELOCITIES[1][i];
velocity[2] += currentCell[i] * LATTICEVELOCITIES[2][i];
}
Note that this is completely untested code - apologies for typos or bugs that need to be fixed, but the basic idea should be sound.
Note also that you should test and benchmark this against equivalent scalar code - modern CPUs typically have two FPUs so there may not be much to be gained from SSE. If you can assume Sandy Bridge/Ivy Bridge/Haswell or later though, then an AVX/AVX2 implementation should do better.
I am writing a C program to generate Keys and test them in an encryption function. However since I have NEVER written a C program before, and I'm completely unused to having manage memory manually, I have run into a problem. I have a memory leak and to be honest, I don't know how to solve it. I understand I need to release the memory at some point but can't until I've run through all keys and I run out of memory before I get through all of them. Writing the program in a different language is NOT an option so please do not suggest that. The code that is leaking is shown below, any help would be appreciated.
EDIT: I know i haven't called a free function to free the memory. I don't see where I can put it because I need the memory until i run through all keys. Putting it outside the loops doesn't solve the problem because the leak occurs inside the loops
2nd EDIT: Posted the full program. I do not have the option of using data structures (i.e. the bool arrays) other than those shown because of how the DES encrypt function(which I did not write) works
#include <stdio.h>
#include <stdlib.h>
#include "des.h"
void dec2bin(bool *testaRR, bool *to_return, int convert);
int main(int argc, const char * argv[])
{
// insert code here...
bool testKey[56] = {
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 1
};
bool testKey2[56] = {//intuitive key reversed for testing
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1, 0
};
bool output[64];
bool input[64] = {//the reverse of below... DES bits are numbered left to right, in order of least to most significant so we must enter the bit values in reverse.
//forexample the binary vale of N is 01001110 but below is displayed as 01110010
1, 0, 0, 0, 1, 1, 0, 0,//1
0, 0, 0, 0, 1, 1, 0, 0,//0
1, 1, 0, 0, 0, 0, 1, 0,//C
1, 0, 1, 0, 0, 0, 1, 0,//E
1, 1, 0, 0, 1, 0, 1, 0,//S
0, 0, 1, 0, 1, 0, 1, 0,//T
1, 0, 1, 0, 0, 0, 1, 0,//E
0, 1, 1, 1, 0, 0, 1, 0 //N
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int i = y-4; i >= 0; i-=4)
printf("%X", input[i]+2*input[i+1]+4*input[i+2]+8*input[i+3]);//this is the conversion to hex code
printf(")\n");
/*
use char[] to store the key as set of
*/
/*bool input[64] = {//this is the given plaintext message in the intuitive order (opposite of what it is)
0, 1, 0, 0, 1, 1, 1, 0,//N
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 1, 0, 1, 0, 0,//T
0, 1, 0, 1, 0, 0, 1, 1,//S
0, 1, 0, 0, 0, 1, 0, 1,//E
0, 1, 0, 0, 0, 0, 1, 1,//C
0, 0, 1, 1, 0, 0, 0, 0,//0
0, 0, 1, 1, 0, 0, 0, 1 //1
};
int y = sizeof(input);
printf("(Input MSG: ");
for (int j = 0; j < y; j+=4)
printf("%X", input[j+3]+2*input[j+2]+4*input[j+1]+8*input[j]);//this is the conversion to hex code
printf(")\n");*/
bool test [8];
bool returned[8];
char keyphrase [8];
keyphrase[7] = 0;
for(int start = 65; start<=90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for(int two = 65; two<=90; two++){
keyphrase[1]=two;
for(int three = 65; three<=90; three++){
keyphrase[2]=three;
for(int four = 65; four<=90; four++){
keyphrase[3]=four;
for(int five = 65;five<=90;five++){
keyphrase[4]=five;
for( int six = 65; six <=90; six++){
keyphrase[5]=six;
for(int seven = 65; seven <=90; seven++){
keyphrase[6]=seven;
printf("%s \n", keyphrase);
}
}}
}
}
}
//once i fix the memory leak I will be calling the EncryptDes Function here and checking the outputblk agains the given cipher text
}
free(keyphrase);
int k = sizeof(testKey);
printf("(Test Key: ");
for (int z = 0; z < k; z+=7)
printf("%d", testKey[z+7]+2*testKey[z+6]+4*testKey[z+5]+8*testKey[z+4]+16*testKey[z+3]+32*testKey[z+2]+64*testKey[z+1]+ 128*testKey[z]);//this is the conversion to hex code
printf(")\n");
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
printf("(Output MSG: ");
for (int i = 0; i < x; i+=4)
printf("%X", output[i+3]+2*output[i+2]+4*output[i+1]+8*output[i]);//this is the conversion to hex code
printf(")\n");
return 0;
}
void dec2bin (bool *testaRR, bool *to_return, int convert)
{
printf("%d : ", convert);
printf("%c", convert);
printf("\n ");
//bool testaRR [8];
for(int st = 0; st<8; st++){
testaRR[st] = convert%2;
to_return[7-st] = testaRR[st];
//printf("%d :", 7-st);
//printf(" %d spot ", st);
convert = convert/2;
//testaRR stores the arrays in one direction
//to_return stores them in the other
//Example:
//65 = 01000001 testaRR least significant on the far right (m0st sig is in index 7)better for storage and keeping track of where the bits actually are in binary
//65 = 10000010 to_return least significant on the far left (same as DES) (most significant bit is index 0) good for printing to screen
}
You do not need dynamic memory management here.
Start with
char keyphrase[8];
keyphrase[7]=0;
instead of your malloc and you will be good to go. Your highest array index is 7 (the terminating NUL), and hence you need an array of 8 items (0..7).
If you really want to use malloc, simply a free() at the end will be fine, but you need to malloc 8 characters and set keyphrase[7] to 0 to do the terminating NUL still.
Here's a tested version that works:
#include <stdio.h>
/* compile with gcc -Wall -std=c99 keyphrase.c -o keyphrase */
int
main (int argc, char **argv)
{
char keyphrase[8];
keyphrase[7] = 0;
for (int start = 65; start <= 90; start++)
{
//dec2bin(test, returned, start);
keyphrase[0] = start;
for (int two = 65; two <= 90; two++)
{
keyphrase[1] = two;
for (int three = 65; three <= 90; three++)
{
keyphrase[2] = three;
for (int four = 65; four <= 90; four++)
{
keyphrase[3] = four;
for (int five = 65; five <= 90; five++)
{
keyphrase[4] = five;
for (int six = 65; six <= 90; six++)
{
keyphrase[5] = six;
for (int seven = 65; seven <= 90; seven++)
{
keyphrase[6] = seven;
printf ("%s \n", keyphrase);
}
}
}
}
}
}
}
}
The real problem is the use of printf. You did not NULL terminate keyphrase, so every time you printf you overflow.
Also, to avoid the memory leak, simply replace char *keyphrase = (char *)malloc(7); with char keyphrase[8];.
You're calling malloc on the first line but I don't see a single free to release what you allocated. After all the loops are completed (i.e. usage of the allocated data is complete) you must call free(keyphrase);
New answer as the program has been modified.
You say your program is using all memory 'because I'm watching the available memory go down from over 4 free GB to about 5 MB'.
I'm guessing the answer is not the loops but these lines:
//loop on the key (starting at
EncryptDES(testKey, output, input, 0);
int x = sizeof(output);
We can't see the source to or declaration of EncryptDES, but you aren't passing a length to it. If 0 is meant to be the length, this would explain it.
The next line however suggests that output is meant to be an array of 64 bytes (rather than 2 strings). But EncryptDES would have no way of knowing this.
I suggest you run the whole thing under valgrind to find out what is happening.
When the Arduino is powered up it has an int array stored in the flash, for example:
int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
When the program button is pressed, it then waits for the piezo to pick up a knock and this array then changes to, for example:
int secretCode[maximumKnocks] = {25, 50, 25, 50, 100, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
(based on http://grathio.com/assets/secret_knock_detector.pde)
How would I write and read the array to/from the EEPROM? This is completely new to me, so any help would be great.
You would write the values using the EEPROM.Write function - loop over the array, writing each value in turn.
Assuming you don't need to store integer values > 254 ( in which case you'd have to write two bytes for each element in secretCode ), this would be:
for ( int i = 0; i < maximumKnocks; ++i )
EEPROM.write ( i, secretCode [ i ] );
Having written them, you would read them back on start-up using the read function in the setup. If the values in the EEPROM are 0xff, which they will be when you first flash the chip, don't copy them into the secret code.
if ( EEPROM.read ( 0 ) != 0xff )
for (int i = 0; i < maximumKnocks; ++i )
secretCode [ i ] = EEPROM.read ( i );
I am implementing uniform LBP in C. But I am confused regarding the concept. I have implemented LBP. Suppose i have an image of 512*512 dimensions. After LBP it will be 510*510. Now how to get 256 bins/pixels from this LBP image.
for(i=1; i < image_src->width - 1; i++)
{
for(j=1; j < image_src->height - 1; j++)
{
const unsigned char center = image_get_pixel_value(image_src, i, j , 0);
unsigned char code = 0;
if(center <= image_get_pixel_value(image_src, i-1, j-1 , 0))
code += 128;
if(center <= image_get_pixel_value(image_src, i-1, j , 0))
code += 64;
if(center <= image_get_pixel_value(image_src, i-1, j+1 , 0))
code += 32;
if(center <= image_get_pixel_value(image_src, i, j+1 , 0))
code += 16;
if(center <= image_get_pixel_value(image_src, i+1, j+1 , 0))
code += 8;
if(center <= image_get_pixel_value(image_src, i+1, j , 0))
code += 4;
if(center <= image_get_pixel_value(image_src, i+1, j-1 , 0))
code += 2;
if(center <= image_get_pixel_value(image_src, i, j-1 , 0))
code += 1;
image_set_pixel_value(image_tar, i-1, j-1, 0, code);
}
}
And this is the lookup table:
int UniformPattern59[16][16] = {
1, 2, 3, 4, 5, 0, 6, 7, 8, 0, 0, 0, 9, 0, 10, 11,
12, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 14, 0, 15, 16,
17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18, 0, 0, 0, 0, 0, 0, 0, 19, 0, 0, 0, 20, 0, 21, 22,
23, 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,
24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0, 0, 27, 0, 28, 29,
30, 31, 0, 32, 0, 0, 0, 33, 0, 0, 0, 0, 0, 0, 0, 34,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 35,
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, 36,
37, 38, 0, 39, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 41,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42,
43, 44, 0, 45, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 47,
48, 49, 0, 50, 0, 0, 0, 51, 52, 53, 0, 54, 55, 56, 57, 58
};
I guess you may misunderstand the concepts about LBP. The LBP has couple of variants: basic LBP, uniform LBP and rotation-invariant uniform LBP.
In the basic LBP, we compare the gray-scale value between the centre pixel and one of its neighbour pixels (interpolated pixels for more accurate result) to get the binary coding (0 or 1) for the bit. Usually, we set the radius of this coding scheme as 1 and the number of neighbours as 8 as default configuration. Therefore, we can get the local binary pattern (LBP) for the centre pixel after comparing all the 8 neighbours, which is a 8-bit binary number, e.g., 01011110 or 11110000. Here, we can see that the range of this kind of LBP is from 0 to 255 if we convert the binary number to decimal number. We usually classify each pixel's LBP code into one of the 256 kinds of patterns to form the histogram for further classification or recognition tasks, which are the 256bins/pixels in your question.
However, there is something different in uniform and rotation-invariant uniform LBPs. That is, the number of patterns is not 256, where the rotation-invariant uniform LBP only has 10 kinds of patterns. The rotation-uniform LBP has 9 rotation-uniform LBPs and 1 others, in which the 9 rotation-uniform LBPs cover 90% patterns over an image usually.
So, in rotation-invariant uniform LBP, you only need to produce a 10-bins histogram for further processing, e.g., classification or recognition. First, you code each pixels as the rotation-invariant uniform LBP, which may produce an image.rows*image.cols matrix. Then, classifying each pattern (the matrix element) to one of the 10 patterns to form an array to represent the histogram.
Some papers for your information:
LBP on Scholarpedia, http://www.scholarpedia.org/article/Local_Binary_Patterns
C++ code by LBP authors, http://www.cse.oulu.fi/CMV/Downloads/LBPSoftware
Multiresolution gray-scale and rotation invariant texture classification with local binary patterns, http://www.rafbis.it/biplab15/images/stories/docenti/Danielriccio/Articoliriferimento/LBP.pdf