In Polars, I'm seeing a return result different than what I would expect when using slicing with series and trying to get the offsets.
I'm creating a Series, then slicing it:
// Make a vec of 3 items, called foo, bar baz
let string_values: Vec<&str> = vec!["foo", "bar", "baz"];
// Add it to a series, this is without dataframes
let series = Series::new("string_values", string_values);
//shape: (3,)
// Series: 'string_values' [str]
// [
// "foo"
// "bar"
// "baz"
// ]
println!("{:?}", series);
This returns a new series.
I can then using downcast_iter() to get the offsets:
// Now we should be able to downcast iter to get the offsets.
// returns [0, 3, 6, 9]
// 0-3 = foo
// 3-6 = bar
// 6-9 = baz
series.utf8().unwrap().downcast_iter().for_each(|array| {
println!("{:?}", array.offsets());
});
Great so far.
I then slice it:
//shape: (2,)
// Series: 'string_values' [str]
// [
// "bar"
// "baz"
// ]
let series_slice = series.slice(1, 2);
println!("{:?}", series_slice);
This returns the correct values.
I then try and use downcast_iter() again:
// Now we should be able to downcast iter to get the offsets for the slice.
// This returns [3, 6, 9]
// Is "foo" still referenced?
series_slice.utf8().unwrap().downcast_iter().for_each(|array| {
println!("{:?}", array.offsets());
});
It returns 3, 6, 9. Why is 9 returned? The length of the series is 6.
Buffers in arrow can be shared. Besides the data they also have an offset and a length.
You original arrow string array contains of the following data:
data: foobarbaz
offsets: 0, 3, 6, 9
offset: 0
length: 3
Retrieving element i uses the following algorithm in pseudocode:
let offset = array.offset
let start_index = offsets[offset + i]
let end_index = offsets[offset + i + 1]
let string_value = data[start_index..end_index]
When you slice an array, we don't copy any data. We only update the offset and the length such that we have all information to represent the sliced array:
data: foobarbaz
offsets: 0, 3, 6, 9
offset: 1
length: 2
I have the following test:
pub(crate) fn from_byte_slice(packet: &[u8]) -> BackendKeyData {
let mut backend = BackendKeyData::new();
backend.pid = i32::from_be_bytes(pop(&packet[5..9]));
backend.key = i32::from_be_bytes(pop(&packet[9..]));
backend
}
#[test]
fn test_backend_key_data() {
let bytes = b"\x75\x00\x00\x00\x12\x00\x00\x149\x241\x17\x173\x241\x137";
let backend = BackendKeyData::from_byte_slice(bytes);
assert_eq!(backend.pid, 0);
}
When I debug the test the byte array changes and becomes this slice:
[0]:117
[1]:0
[2]:0
[3]:0
[4]:18
[5]:0
[6]:0
[7]:20
[8]:57
[9]:36
[10]:49
[11]:23
[12]:23
[13]:51
[14]:36
[15]:49
[16]:19
[17]:55
What's going on here, why the change?
\x is for 2 digit hexadecimal escapes only. \x137 does not make a byte with value 137, but instead a byte with value 0x13 followed by the byte represented by the ascii character 7. Use an array literal for base 10 values instead:
&[75, 0, 0, 0, 12, 0, 0, 149, 241, 17, 173, 241, 137]
I have two arrays of values:
t = [0; 1; 2];
q = [0; 100; 200];
I need those to be one string that's like:
str = '0, 0, 1, 100, 2, 200';
I can't see a nice way to do it in MATLAB (R2017a) without using a loop. I'd like to avoid that if possible as there's a pretty large array of values and a lot of files and it'll take forever.
Any ideas?
Combine compose with strjoin:
t = [0; 1; 2];
q = [0; 100; 200];
str = strjoin(compose('%d', [t(:)'; q(:)']), ', ');
Output:
str =
'0, 0, 1, 100, 2, 200'
For non integer numbers, use: %f instead of %d
Here's a possible approach. This works for integer numbers, or if you want a fixed number of decimals in the string representation:
t = [0; 1; 2];
q = [0; 100; 200];
tq = reshape([t(:).'; q(:).'], 1, []);
s = sprintf('%i, ',tq); % or change '%i' to something like '%.5f'
s = s(1:end-2)
Result:
s =
'0, 0, 1, 100, 2, 200'
If you have non-integer numbers and want the number of decimals in the representation to be chosen automatically, you can use mat2str instead of sprintf, but then you need to deal with the spaces using regexpre or a similar function:
t = [0; 1; 2];
q = [0; 100; 200];
tq = reshape([t(:).'; q(:).'], 1, [])
s = regexprep(num2str(tq), '\s+', ', ');
I'm trying to scroll some text larger than the screen.
The docs say newpad is not limited by the screen size, but initiating it with values greater than the terminal available columns or lines fails to print anything:
newpad(LINES + 1, COLS); // fails
newpad(LINES, COLS); // works
Entire code for reference:
extern crate ncurses;
use ncurses::*;
fn main() {
initscr();
start_color();
use_default_colors();
cbreak();
noecho();
curs_set(CURSOR_VISIBILITY::CURSOR_INVISIBLE);
let pad = newpad(1000, COLS);
refresh();
let mut x = 0;
while x < 1000 {
x += 1;
wprintw(pad, &format!("Line number {}\n", x));
}
prefresh(pad, 0, 0, 0, 0, LINES, COLS);
getch();
endwin();
}
The behavior is a bit odd.
If the number of lines or rows is greater than the viewport, the last two prefresh arguments must be at most LINES - 1 and COLS - 1 respectively:
prefresh(pad, 0, 0, 0, 0, LINES - 1, COLS - 1);
If it's less, there's no need to subtract 1, as the code will work as expected.
What's the fewest number of steps needed to draw all of the cube's vertices, without picking up the pen from the paper?
So far I have reduced it to 16 steps:
0, 0, 0
0, 0, 1
0, 1, 1
1, 1, 1
1, 1, 0
0, 1, 0
0, 0, 0
1, 0, 0
1, 0, 1
0, 0, 1
0, 1, 1
0, 1, 0
1, 1, 0
1, 0, 0
1, 0, 1
1, 1, 1
I presume it can be reduced less than 16 steps as there are only 12 vertices to be drawn
You can view a working example in three.js javascript here:
http://jsfiddle.net/kmturley/5aeucehf/show/
Well I encoded a small brute force solver for this
the best solution is with 16 vertexes
took about 11.6 sec to compute
all is in C++ (visualization by OpenGL)
First the cube representation:
//---------------------------------------------------------------------------
#define a 0.5
double pnt[]=
{
-a,-a,-a, // point 0
-a,-a,+a,
-a,+a,-a,
-a,+a,+a,
+a,-a,-a,
+a,-a,+a,
+a,+a,-a,
+a,+a,+a, // point 7
1e101,1e101,1e101, // end tag
};
#undef a
int lin[]=
{
0,1,
0,2,
0,4,
1,3,
1,5,
2,3,
2,6,
3,7,
4,5,
4,6,
5,7,
6,7,
-1,-1, // end tag
};
// int solution[]={ 0, 1, 3, 1, 5, 4, 0, 2, 3, 7, 5, 4, 6, 2, 6, 7, -1 }; // found polyline solution
//---------------------------------------------------------------------------
void draw_lin(double *pnt,int *lin)
{
glBegin(GL_LINES);
for (int i=0;lin[i]>=0;)
{
glVertex3dv(pnt+(lin[i]*3)); i++;
glVertex3dv(pnt+(lin[i]*3)); i++;
}
glEnd();
}
//---------------------------------------------------------------------------
void draw_pol(double *pnt,int *pol)
{
glBegin(GL_LINE_STRIP);
for (int i=0;pol[i]>=0;i++) glVertex3dv(pnt+(pol[i]*3));
glEnd();
}
//---------------------------------------------------------------------------
Now the solver:
//---------------------------------------------------------------------------
struct _vtx // vertex
{
List<int> i; // connected to (vertexes...)
_vtx(){}; _vtx(_vtx& a){ *this=a; }; ~_vtx(){}; _vtx* operator = (const _vtx *a) { *this=*a; return this; }; /*_vtx* operator = (const _vtx &a) { ...copy... return this; };*/
};
const int _max=16; // know solution size (do not bother to find longer solutions)
int use[_max],uses=0; // temp line usage flag
int pol[_max],pols=0; // temp solution
int sol[_max+2],sols=0; // best found solution
List<_vtx> vtx; // model vertexes + connection info
//---------------------------------------------------------------------------
void _solve(int a)
{
_vtx *v; int i,j,k,l,a0,a1,b0,b1;
// add point to actual polyline
pol[pols]=a; pols++; v=&vtx[a];
// test for solution
for (l=0,i=0;i<uses;i++) use[i]=0;
for (a0=pol[0],a1=pol[1],i=1;i<pols;i++,a0=a1,a1=pol[i])
for (j=0,k=0;k<uses;k++)
{
b0=lin[j]; j++;
b1=lin[j]; j++;
if (!use[k]) if (((a0==b0)&&(a1==b1))||((a0==b1)&&(a1==b0))) { use[k]=1; l++; }
}
if (l==uses) // better solution found
if ((pols<sols)||(sol[0]==-1))
for (sols=0;sols<pols;sols++) sol[sols]=pol[sols];
// recursion only if pol not too big
if (pols+1<sols) for (i=0;i<v->i.num;i++) _solve(v->i.dat[i]);
// back to previous state
pols--; pol[pols]=-1;
}
//---------------------------------------------------------------------------
void solve(double *pnt,int *lin)
{
int i,j,a0,a1;
// init sizes
for (i=0;i<_max;i++) { use[i]=0; pol[i]=-1; sol[i]=-1; }
for(i=0,j=0;pnt[i]<1e100;i+=3,j++); vtx.allocate(j); vtx.num=j;
for(i=0;i<vtx.num;i++) vtx[i].i.num=0;
// init connections
for(uses=0,i=0;lin[i]>=0;uses++)
{
a0=lin[i]; i++;
a1=lin[i]; i++;
vtx[a0].i.add(a1);
vtx[a1].i.add(a0);
}
// start actual solution (does not matter which vertex on cube is first)
pols=0; sols=_max+1; _solve(0);
sol[sols]=-1; if (sol[0]<0) sols=0;
}
//---------------------------------------------------------------------------
Usage:
solve(pnt,lin); // call once to compute the solution
glColor3f(0.2,0.2,0.2); draw_lin(pnt,lin); // draw gray outline
glColor3f(1.0,1.0,1.0); draw_pol(pnt,sol); // overwrite by solution to visually check correctness (Z-buffer must pass also on equal values!!!)
List
is just mine template for dynamic array
List<int> x is equivalent to int x[]
x.add(5) ... adds 5 to the end of list
x.num is the used size of list in entries
x.allocate(100) preallocate list size to 100 entries (to avoid relocations slowdowns)
solve(pnt,lin) algorithm
first prepare vertex data
each vertex vtx[i] corresponds to point i-th point in pnt table
i[] list contains the index of each vertex connected to this vertex
start with vertex 0 (on cube is irrelevant the start point
otherwise there would be for loop through every vertex as start point
_solve(a)
it adds a vertex index to actual solution pol[pols]
then test how many lines is present in actual solution
and if all lines from lin[] are drawn and solution is smaller than already found one
copy it as new solution
after test if actual solution is not too long recursively add next vertex
as one of the vertex that is connected to last vertex used
to limit the number of combinations
at the end sol[sols] hold the solution vertex index list
sols is the number of vertexes used (lines-1)
[Notes]
the code is not very clean but it works (sorry for that)
hope I did not forget to copy something