Code golf: the Mandelbrot set - code-golf

Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
Usual rules for the code golf. Here is an implementation in python as an example
from PIL import Image
im = Image.new("RGB", (300,300))
for i in xrange(300):
print "i = ",i
for j in xrange(300):
x0 = float( 4.0*float(i-150)/300.0 -1.0)
y0 = float( 4.0*float(j-150)/300.0 +0.0)
x=0.0
y=0.0
iteration = 0
max_iteration = 1000
while (x*x + y*y <= 4.0 and iteration < max_iteration):
xtemp = x*x - y*y + x0
y = 2.0*x*y+y0
x = xtemp
iteration += 1
if iteration == max_iteration:
value = 255
else:
value = iteration*10 % 255
print value
im.putpixel( (i,j), (value, value, value))
im.save("image.png", "PNG")
The result should look like this
Use of an image library is allowed. Alternatively, you can use ASCII art. This code does the same
for i in xrange(40):
line = []
for j in xrange(80):
x0 = float( 4.0*float(i-20)/40.0 -1.0)
y0 = float( 4.0*float(j-40)/80.0 +0.0)
x=0.0
y=0.0
iteration = 0
max_iteration = 1000
while (x*x + y*y <= 4.0 and iteration < max_iteration):
xtemp = x*x - y*y + x0
y = 2.0*x*y+y0
x = xtemp
iteration += 1
if iteration == max_iteration:
line.append(" ")
else:
line.append("*")
print "".join(line)
The result
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
**************************************** ***************************************
*************************************** **************************************
************************************* ************************************
************************************ ***********************************
*********************************** **********************************
************************************ ***********************************
************************************* ************************************
*********************************** **********************************
******************************** *******************************
**************************** ***************************
***************************** ****************************
**************************** ***************************
************************ * * ***********************
*********************** * * **********************
******************** ******* ******* *******************
**************************** ***************************
****************************** *****************************
***************************** * * * ****************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
********************************************************************************
Edit:
Rules for the ASCII art:
size in rows/columns is parametrized and the code must work with any valid value.
at least three level of differentiation in density depending on the iteration count (so my prototype up there is not compliant)
oriented horizontally (so my prototype up there is not compliant)
critical parameters are fixed (max iteration = 1000, runaway value xx + yy <= 4.0)
Rules for the graphic:
size in rows/columns is parametrized and the code must work with any valid value.
at least three level of colors, gray scale
oriented horizontally (my prototype is compliant)

There was a perl solution already some years ago
posted in perlmonks, it reads:
#!/usr/bin/perl
$r=25; $c=80;
$xr=6;$yr=3;$xc=-0.5;$dw=$z=-4/
100;local$";while($q=$dr=rand()
/7){$w+=$dw;$_=join$/,map{$Y=$_*
$yr/$r;
join"" ,map{$ x=$_*$
xr/$c;($ x,$y)= ($xc+$x
*cos($ w)-$Y* sin$w,$yc+
$x*sin ($w)+$Y*cos
$w);$ e=-1;$ a=$b=0
;($a,$b) =($u-$v+$x,2*$a* $b+$y) while(
$ u=$a*$ a)+($v=$b*$b)<4.5 &&++$e <15;if (($e>$
q&&$e< 15)||($e==$q and rand() <$dr)) {$q=$e;($d0,$d1) =($x,$
y); } chr(+( 32,96,+ 46,45,43,58,73,37 ,36,64
,32)[$ e/1.5] );}(-$ c/2)..($c/2)-1;} (-$r/2
)..($ r/2)-1;select$", $",$", 0.015; system
$^O=~m ~[wW]in~x?"cls": "clear";print ;$xc=(
$d0+15 *$xc)/16;$yc=($ d1+15*$yc)/ 16;$_*=
1+$z for $xr,$yr;$dw *=-1 if rand
()<0.02; (++$i%110 )||($z*=-1)}
which is a "Mandelbrot explorer".
(It rotates, zooms in & out, and scrolls randomly to examine regions
of the Mandelbrot set it deems "interesting.", according to
it's creator.)
It doesn't exactly follow the specs named here but
makes an interesting entry (imho). Maybe a simple
Mandlebrot is not very interesting for the perl gods ;.-)
Regards
rboo

J, greyscale graphical output with levels, 170 characters.
load'viewmat'
c=:>(<:;|:;0$])((2$>:#+:)$-:%~i:)300
(2.5*|:3 99$i._99)viewmat {:99&<.#;#(({:+4&<#:({.+1&{)#:*:);~(0{c)&+#({.-1&{)#:*:,:(1{c)&+#:+:#({.*1&{))^:99 c
alt text http://i40.tinypic.com/2i7lm0.jpg
J, graphical output with levels, 151 characters.
load'viewmat'
c=:>(<:;|:;0$])((2$>:#+:)$-:%~i:)99
viewmat {:99&<.#;#(({:+4&<#:({.+1&{)#:*:);~(0{c)&+#({.-1&{)#:*:,:(1{c)&+#:+:#({.*1&{))^:99 c
Output:
alt text http://i40.tinypic.com/6ynxap.jpg
J, graphical output, 124 characters.
load'viewmat'
c=:>(<:;|:)((2$>:#+:)$-:%~i:)200
viewmat 4>:({.+{:)#:*:(5<.>#(({.c)&+#({.-{:)#:*:;({:c)&+#:+:#({.*{:)))^:999 c
Basically just runs "viewmat" on the ascii output.
alt text http://i40.tinypic.com/etv5lf.jpg
J, ASCII, 101 characters.
c=:>(<:;|:)((2$>:#+:)$-:%~i:)20
4>:({.+{:)#:*:(5<.>#(({.c)&+#({.-{:)#:*:;({:c)&+#:+:#({.*{:)))^:999 c
Wasted 6 characters to properly parametrize the size.
J, ASCII, 95 characters.
c=:>(<:;|:)41 41$10%~i:20
4>:({.+{:)#:*:(5<.>#(({.c)&+#({.-{:)#:*:;({:c)&+#:+:#({.*{:)))^:999 c
It doesn't do multiple levels of greyscale yet.
Explanation (currently out of date, will update later):
i:20
Generate the list of integers from -20 to 20.
10%~i:20
Divide them all by 10 (% is divide, ~ is invert order of arguments.
41 41$10%~i:20
Fill this list into a 41x41 array, with wrapping.
(-&1;|:)41 41$`10%~i:20`
Append to this array a second array of the same size, but transposed. -&1 subtracts one from each element, ; is append, |: is transpose. Now we have a 2x41x41 array, where the first 41x41 array contains the "x0" values of each coordinate and the second contains the "y0" values.
4<:~({.+{:)#:*:(5<.>#(({.c)&+#:({.-{:)#:*:;({:c)&+#:+:#:({.*{:)))^:1000 ($c)$0
Wow. Let's break this up from the right.
($c)$0
This creates a second 2x41x41 array (the same size as c), filled with 0s. This is going to be the variable we're iterating on - basically the first array contains the "x" values and the second contains the "y" values.
^:1000 means "Repeat the previous thing in parentheses 1000 times. This is our loop counter.
(5<.>#(({.c)&+#:({.-{:)#:*:;({:c)&+#:+:#:({.*{:))
This is the next big chunk. It's basically doing one iteration of the loop. First we generate x with (({.c)&+#:({.-{:)#:*:, then join ; it to y ({:c)&+#:+:#:({.*{:)
(({.c)&+#:({.-{:)#:*:
This generates x (remember it's operating on that 2x41x41 array representing x and y). First it squares *: each element, then does {. - {:, or x^2 - y^2. {. selects the first 41x41 array and {: the second. #: joins functions together.
To x^2-y^2, we need to add x0 - this is the first 41x41 array of c so we can do this with ({.c)&+ - & curries {.c (x0) into +.
({:c)&+#:+:#:({.*{:)
We generate y in a similar fashion. First we pairwise multiply the input x {. and y {: arrays, double the result with +:, and add y0 {:c.
The last step of this function is simply 5<.> - unboxing the join of the two new x and y arrays, and capping at 5 - it's ok since this function is monotonically increasing above 4, and we'll get out-of-bounds integers if we don't cap the values on each iteration.
4>:({.+{:)#:*:
The last step, once we've finished iterating. We square the x and y arrays *:, add them together {.+{:, and make a boolean array that's true for every element that's >: less than 4.
Output:
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 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 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 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 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 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 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 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 0 0 0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 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 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 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 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 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 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 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 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 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 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 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 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 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 0 0 0

Python 108 (was 122):
for a in range(900):print"\n.x"[(a%30>0)+(abs(reduce(lambda z,c:z*z+c,[a%30*.1-2+1j*(a/30*.1-1.5)]*30))<2)],
That last "30" is the iteration count, not one of the dimensions.
Output looks thus:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . x . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . x x . . . . . . . . . .
. . . . . . . . . . . . . . . . . x x . . . . . . . . . .
. . . . . . . . . . . . . . x . x x x x x . . . . . . . .
. . . . . . . . . . . . . . x x x x x x x x x . . . . . .
. . . . . . . . . . . . . x x x x x x x x x x . . . . . .
. . . . . . . . . x . . . x x x x x x x x x x . . . . . .
. . . . . . . . x x x . x x x x x x x x x x x x . . . . .
. . . . . . . x x x x x x x x x x x x x x x x . . . . . .
x x x x x x x x x x x x x x x x x x x x x x . . . . . . .
. . . . . . . x x x x x x x x x x x x x x x x . . . . . .
. . . . . . . . x x x . x x x x x x x x x x x x . . . . .
. . . . . . . . . x . . . x x x x x x x x x x . . . . . .
. . . . . . . . . . . . . x x x x x x x x x x . . . . . .
. . . . . . . . . . . . . . x x x x x x x x x . . . . . .
. . . . . . . . . . . . . . x . x x x x x . . . . . . . .
. . . . . . . . . . . . . . . . . x x . . . . . . . . . .
. . . . . . . . . . . . . . . . . x x . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . x . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Now going to update based on the updated rules....

Haskell (185 chars, with all the features required in the question)
Using complex numbers and actually counting the required number of iterations (unlike the "simple" version in my other answer).
import Complex
i v=show(length$takeWhile((<=4).magnitude)$take 1000$iterate(\c->c*c+v)v)!!0
r t=[-2,4/read t-2..2]
main=getLine>>=(\[s,t]->mapM_ putStrLn[[i$x:+y|x<-r s]|y<-r t]).words
The dimensions of the resulting image are read from stdin. The output is "colored" according to the number of required iterations:
$ ./fract
60 30
1111111111111111111111111111111111111111111111111111111111111
1111111111111112222222222222222111111111111111111111111111111
1111111112222222222222222222222222222111111111111111111111111
1111112222222222222222222222222222222222111111111111111111111
1112222222222222222222222222222222222222222111111111111111111
1222222222222222222222333333333322222222222221111111111111111
2222222222222222223333333344443333332222222222211111111111111
2222222222222233333333344458755443333322222222222111111111111
2222222222233333333344445568117544433333222222222211111111111
2222222233333333344445556711111765544333322222222221111111111
2222233333333444445562391112161568786443332222222222111111111
2223333334444455556681111111111111157544333222222222211111111
2333334445617677777911111111111111111854333222222222211111111
3333444456681131231111111111111111111654333322222222221111111
3444555671111111111111111111111111114644333322222222221111111
2569888111111111111111111111111111296544333322222222221111111
3444555671111111111111111111111111114644333322222222221111111
3333444456681131231111111111111111111654333322222222221111111
2333334445617677777911111111111111111854333222222222211111111
2223333334444455556681111111111111157544333222222222211111111
2222233333333444445562391112161568786443332222222222111111111
2222222233333333344445556711111765544333322222222221111111111
2222222222233333333344445568117544433333222222222211111111111
2222222222222233333333344458755443333322222222222111111111111
2222222222222222223333333344443333332222222222211111111111111
1222222222222222222222333333333322222222222221111111111111111
1112222222222222222222222222222222222222222111111111111111111
1111112222222222222222222222222222222222111111111111111111111
1111111112222222222222222222222222222111111111111111111111111
1111111111111112222222222222222111111111111111111111111111111
1111111111111111111111111111111111111111111111111111111111111

Delphi - 310 249 239 224 chars
ASCII version. Uses 7 levels for gradient shading.
Removed program P; and {$APPTYPE CONSOLE} according to (suggested by Uwe Raabe);
Inlined 1000 const (suggested by Uwe Raabe);
Inlined '8Oo;,. ' variable;
Changed word to integer so that the loop variable can be negative, and then changed the loop range for I from 3..38 to -5..30 so that (I-8) could be replaced by I (suggested by Eric Grange);
Changed 1000 to 1e3 (suggested by Eric Grange)
Changed type Double to Real (suggested by Luc Neville via e-mail)
Removed brackets while((x*x+y*y<=4)and(t<1000))do --> while(x*x+y*y<=4)and(t<1000)do (suggested by Luc Neville via e-mail)
Removed spaces between numbers that were followed by a keyword (so that for j:=-5 to 30 do becomes for j:=-5to 30do(Suggested by Neville via e-mail)
Changed loop range to for j:=8to 40do, removing the negative sign, and changed integer back to word. Compensated this offset in the formula, by changing x * x - y * y + i / 16 - 2; to x * x - y * y + i / 16 - 3;
Unreadable version:
var n,x,y:Real;t,i,j:Word;begin for j:=8to 40do begin for i:=8to 65do begin x:=0;y:=0;t:=0;while(x*x+y*y<=4)and(t<1e3)do begin n:=x*x-y*y+i/16-3;y:=2*x*y+j/12-2;x:=n;inc(t)end;Write('8Oo;,. '[t mod 7+1])end;WriteLn;end;end.
Formatted code:
var
n, x, y: Real;
t, i, j: Word;
begin
for j := 8 to 40 do
begin
for i := 8 to 65 do
begin
x := 0;
y := 0;
t := 0;
while (x * x + y * y <= 4) and (t < 1e3) do
begin
n := x * x - y * y + i / 16 - 3;
y := 2 * x * y + j / 12 - 2;
x := n;
inc(t)
end;
Write('8Oo;,. '[t mod 7 + 1])
end;
WriteLn;
end;
end.
Output:
OOOOOOOOOOOOOOOOOooooooooooooooooooooooooooooooooooooooooo
OOOOOOOOOOOOOOOOooooooooo;;;;;;;;;;;;;;;oooooooooooooooooo
OOOOOOOOOOOOOOOoooooo;;;;;;;;;;;;;;,,,,,;;;;oooooooooooooo
OOOOOOOOOOOOOOooooo;;;;;;;;;;;;;,,,, ; .,,,;;;oooooooooooo
OOOOOOOOOOOOOoooo;;;;;;;;;;;;;,,,,.. O ,,,,;;;oooooooooo
OOOOOOOOOOOOooo;;;;;;;;;;;;;,,,,,...8oo,8..,,,;;;;oooooooo
OOOOOOOOOOOooo;;;;;;;;;;;;,,,,,,.. Oo. ; 8...,,;;;;ooooooo
OOOOOOOOOOOoo;;;;;;;;;;;;,,,,,. 8OO O8 ..,,;;;;oooooo
OOOOOOOOOOoo;;;;;;;;;;;,,,,.. O,oO8oo ,OOoO88,.,;;;;ooooo
OOOOOOOOOOo;;;;;;;;;;,,,.... o. . OOO ,;;;;;oooo
OOOOOOOOOO;;;;;;;;;,,...... o., o .,;;;;;ooo
OOOOOOOOOo;;;;;;,,. o 88O. ;O.,;;;;;ooo
OOOOOOOOO;;;,,,,,.. ,,o;,oOo; o ,,;;;;;oo
OOOOOOOOO;,,,,,... 8o; 8;, .,,;;;;;oo
OOOOOOOOO,,,,,... Oo . 8.,,;;;;;oo
OOOOOOOOO,.,. 88oo o , , .,,;;;;;oo
OOOOOOOO ,8 .,,;;;;;;o
OOOOOOOOO,.,. 88oo o , , .,,;;;;;oo
OOOOOOOOO,,,,,... Oo . 8.,,;;;;;oo
OOOOOOOOO;,,,,,... 8o; 8;, .,,;;;;;oo
OOOOOOOOO;;;,,,,,.. ,,o;,oOo; o ,,;;;;;oo
OOOOOOOOOo;;;;;;,,. o 88O. ;O.,;;;;;ooo
OOOOOOOOOO;;;;;;;;;,,...... o., o .,;;;;;ooo
OOOOOOOOOOo;;;;;;;;;;,,,.... o. . OOO ,;;;;;oooo
OOOOOOOOOOoo;;;;;;;;;;;,,,,.. O,oO8oo ,OOoO88,.,;;;;ooooo
OOOOOOOOOOOoo;;;;;;;;;;;;,,,,,. 8OO O8 ..,,;;;;oooooo
OOOOOOOOOOOooo;;;;;;;;;;;;,,,,,,.. Oo. ; 8...,,;;;;ooooooo
OOOOOOOOOOOOooo;;;;;;;;;;;;;,,,,,...8oo,8..,,,;;;;oooooooo
OOOOOOOOOOOOOoooo;;;;;;;;;;;;;,,,,.. O ,,,,;;;oooooooooo
OOOOOOOOOOOOOOooooo;;;;;;;;;;;;;,,,, ; .,,,;;;oooooooooooo
OOOOOOOOOOOOOOOoooooo;;;;;;;;;;;;;;,,,,,;;;;oooooooooooooo
OOOOOOOOOOOOOOOOooooooooo;;;;;;;;;;;;;;;oooooooooooooooooo
OOOOOOOOOOOOOOOOOooooooooooooooooooooooooooooooooooooooooo

T-SQL (421 408 characters)
declare #s varchar(max);
with
x(x)as(select CAST(-2.0 as real)union all select x+.01 from x where x<1),
y(y)as(select CAST(-1.1 as real)union all select y+.02 from y where y<1.1),
g(x,y,u,v,g)as(select x,y,x*0,y*0,0 from x,y union all select x,y,u*u-v*v+x,2*u*v+y,g+1 from g where g<24and u*u+v*v<4),
m(x,y,v)as(select x,y,case when x<-1.99 then char(10)end+substring(' .-''^"+oiexp**##$$$§§%%%#',COUNT(*),1)from g group by x,y)
select #s=#s+v from m order by y,x option(maxrecursion 400)
select #s
Output is here:

awk - 134 135 chars
ASCII art, fully compliant. Fairly straightforward translation of the python reference implementation:
{for(j=-2;j<=2;j+=4/$2){for(i=-3;i<=1;i+=4/$1){for(x=y=k=0;x*x+y*y<=4&&++k<4^5;)
{t=x*x-y*y+i;y=2*x*y+j;x=t}printf"%d",log(k)}print""}}
test run
$ awk -f mandel.awk
10 10
00000000000
00000000000
00000111000
00001116110
00011166610
00046666611
00011166610
00001116110
00000111000
00000000000
00000000000
and this is a VT-100 art. Set the xterm to "unreadable" characters, then try a 400x200 set:
{for(j=-2;j<=2;j+=4/$2){for(i=-3;i<=1;i+=4/$1){for(x=y=k=0;x*x+y*y<=4&&++k<1000;)
{t=x*x-y*y+i;y=2*x*y+j;x=t}printf"\x1b[4%dm ",log(k)}print"\x1b[0m"}}

Haskell (162 characters)
A (fairly) straight forward ASCII art version:
c(x,y)|x*x+y*y<4.0='x'
|True='.'
i v w=c(iterate s(v,w)!!1000)where s(x,y)=(x*x-y*y+v,2*x*y+w)
main=mapM_ putStrLn[[i x y|x<-[-1.5,-1.45..0.5]]|y<-[1,0.9.. -1]]
Output:
$ ./a.out
.........................................
.........................................
..........................xxxx...........
..........................xxxx...........
....................x..xxxxxxxxxx........
....................xxxxxxxxxxxxxxxxx....
..................xxxxxxxxxxxxxxxxxxx....
.................xxxxxxxxxxxxxxxxxxxxx...
.......xxxxxxx..xxxxxxxxxxxxxxxxxxxxxx...
......xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx...
..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.....
......xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx...
.......xxxxxxx..xxxxxxxxxxxxxxxxxxxxxx...
.................xxxxxxxxxxxxxxxxxxxxx...
..................xxxxxxxxxxxxxxxxxxx....
....................xxxxxxxxxxxxxxxxx....
....................x..xxxxxxxxxx........
..........................xxxx...........
..........................xxxx...........
.........................................
.........................................
More readable version:
outchar (x,y)
| x*x + y*y < 4.0 = 'x'
| otherwise = '.'
calc v w = outchar (iterate step (v,w) !! 1000)
where step (x,y) = (x*x - y*y + v, 2*x*y + w)
main = mapM_ putStrLn [[calc x y
| x <- [-1.5, -1.45 .. 0.5]]
| y <- [1, 0.9 .. -1]]

Python 197 162
def m():
for a in range(900):
c=complex(a%30*.1-2,a/30*.1-1.5);z,i=c-c,0
while i<28 and abs(z)<2:z,i=z*z+c,i+1
if a%30==0:print
print ".-+oxawOX "[i/3],
I'm interested in how you get rid of the white space in python.

Assembler
.COM Executable = 140 bytes
Source = 652 bytes
mov ax,13h
int 10h
mov bx,200
xor di,di
mov ax,0a000h
mov es,ax
l1:mov bp,320
mov [si],bx
fild w[si]
fisub w[v100]
fidiv w[v50]
l2:mov cx,1000
mov [si],bp
fild w[106h]
fisub w[si]
fidiv w[v80]
fld1
fsubp
fldz
fldz
l3:fld st(1)
fmul st(0)
fld st(1)
fmul st(0)
fld st(0)
fadd st(2)
fcomp d[v4]
fstsw ax
test ah,45h
jz l4
fsubp
fadd st(3)
fxch st(2)
fild w[v2]
fmulp st(1)
fmulp st(1)
fadd st(3)
loop l3
mov al,255
l5:fcompp
fcomp
stosb
dec bp
jnz l2
fcomp
dec bx
jnz l1
mov ah,7
int 21h
mov ax,3
int 10h
ret
l4:mov ax,-10
mul cx
fcompp
jmp l5
v100:dw 100
v50:dw 80
v80:dw 90
v4:dd 4.0
v2:dw 2

JavaScript (168/174 characters)
Rhino version (168 characters):
m=1000;for(i=0;i<40;i++){l=[];for(j=0;j<80;j++){v=j/80*3-2,w=2-i/40*3,x=y=c=0;while(x*x+y*y<5&&c<m)t=x*x-y*y+v,y=2*x*y+w,x=t,c++;l.push(c==m?' ':'-')}print(l.join(''))}
Firebug version (174 characters):
m=1000;for(i=0;i<40;i++){l=[];for(j=0;j<80;j++){v=j/80*3-2,w=2-i/40*3,x=y=c=0;while(x*x+y*y<5&&c<m)t=x*x-y*y+v,y=2*x*y+w,x=t,c++;l.push(c==m?' ':'-')}console.log(l.join(''))}

And a dc entry - 152 chars
My usual madness in dc
Ak?4r/sa4r/sbA00sm[0*]sG_2sj[_3si[0ddsxsysk[lxd*lyd*-li+2lxly**lj+sysx1lxd*lyd*+
4!>G1lk1+dsklm<G*1=L]dsLx0klk1-vvnAklila+dsi1!<I]dsIxAPljlb+dsj2!<J]dsJx
test run:
bash-3.2$ dc -f mandel.dc
10 20
000000000000000000000
000000000011111111111
000000001111111111111
000000111111115211111
000000111111555551111
000000555555555551111
000000111111555551111
000000111111115211111
000000001111111111111
000000000011111111111
000000000000000000000

Ruby - 139 chars (Twitter-friendly)
b,a=$*;a.to_i.times{|l|puts (0..b.to_i).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*""}
$ ruby mandelbrot.rb 80 32
or as a single command line (134 chars including ruby -e)
ruby -e "32.times{|l|puts (0..78).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*''}"
"###############################$$$$$$$$$$$%%%%&&')*/))*.o&%$$$$$$$$########"""""
############################$$$$$$$$$$$$$%%%%%&''((*-G,*('&&%%$$$$$$$$$########""
#########################$$$$$$$$$$$$$%%%%%&&'929-03ooL.+0:'&%%%%%$$$$$$$########
#####################$$$$$$$$$$$$$$%%%&&&&&''()-1oooooooo/*('&&%%%%%%%$$$$$######
##################$$$$$$$$$$$$%%%&'''''''''())*,6oooooooo.*)((''&&&&&&&&%$$$#####
###############$$$$$$$$$%%%%%%&&'(+3:0.,++ooE6ooooooooooo?8o1j?8)(((()4/'&%$$$###
###########$$$$$$%%%%%%%%%%&&&&''(*1>ooo8oooooooooooooooooooooooo.3oooD/,'%%$$$##
#######$$$$%%%%%%%%%%%%%&&&&&&')8,,17ooooooooooooooooooooooooooooooooQ0)'&&%%$$$#
$$$$$$%&)(&&&&&&&&&&&&&&&'''((*18oooooooooooooooooooooooooooooooooooool*(('&%%$$$
$$%%%%&&*2*))((((25*(('''((()+F>=oooooooooooooooooooooooooooooooooooooooo1.(%%$$$
%%%%%&&'()*,1o47//5o5/?,+***+3oooooooooooooooooooooooooooooooooooooooooo.)(&%%$$$
%%%%&'''(*+-3ooooooooooom//-.oooooooooooooooooooooooooooooooooooooooooooo9+&%%$$$
&&(*+)))+-?ooooooooooooooooL5ooooooooooooooooooooooooooooooooooooooooooo8)&%%%$$$
'()*-X=U95ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*'&&%%%$$$
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo:.+)('&&%%%$$$
'()*-X=U95ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo*'&&%%%$$$
&&(*+)))+-?ooooooooooooooooL5ooooooooooooooooooooooooooooooooooooooooooo8)&%%%$$$
%%%%&'''(*+-3ooooooooooom//-.oooooooooooooooooooooooooooooooooooooooooooo9+&%%$$$
%%%%%&&'()*,1o47//5o5/?,+***+3oooooooooooooooooooooooooooooooooooooooooo.)(&%%$$$
$$%%%%&&*2*))((((25*(('''((()+F>=oooooooooooooooooooooooooooooooooooooooo1.(%%$$$
$$$$$$%&)(&&&&&&&&&&&&&&&'''((*18oooooooooooooooooooooooooooooooooooool*(('&%%$$$
#######$$$$%%%%%%%%%%%%%&&&&&&')8,,17ooooooooooooooooooooooooooooooooQ0)'&&%%$$$#
###########$$$$$$%%%%%%%%%%&&&&''(*1>ooo8oooooooooooooooooooooooo.3oooD/,'%%$$$##
###############$$$$$$$$$%%%%%%&&'(+3:0.,++ooE6ooooooooooo?8o1j?8)(((()4/'&%$$$###
##################$$$$$$$$$$$$%%%&'''''''''())*,6oooooooo.*)((''&&&&&&&&%$$$#####
#####################$$$$$$$$$$$$$$%%%&&&&&''()-1oooooooo/*('&&%%%%%%%$$$$$######
#########################$$$$$$$$$$$$$%%%%%&&'929-03ooL.+0:'&%%%%%$$$$$$$########
############################$$$$$$$$$$$$$%%%%%&''((*-G,*('&&%%$$$$$$$$$########""
"###############################$$$$$$$$$$$%%%%&&')*/))*.o&%$$$$$$$$########"""""
"""""###############################$$$$$$$$$%%%&'*)'&&%%%$$$$$$$#######"""""""""
"""""""""#################################$$$$$$$%%%%%$$$$$#########"""""""""""""
""""""""""""""""#############################################""""""""""""""""""""

Python. 98 chars.
for _ in range(526):z=0;exec"z=z*z+_%25*.1-2+_/25*.1j-1j;"*50;print[".#"[z.real<4],"\n"][_%25<1],
Prints something like this:
. . . . . . . . . . . . . . . . . . . # . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . # # . . . . .
. . . . . . . . . . . . . . . . . # # . . . . .
. . . . . . . . . . . . . . # . # # # # # . . .
. . . . . . . . . . . . . . # # # # # # # # # .
. . . . . . . . . . . . . # # # # # # # # # # .
. . . . . . . . . . . . . # # # # # # # # # # .
. . . . . . . . # # # . # # # # # # # # # # # .
. . . . . . . # # # # # # # # # # # # # # # # .
# # # # # # # # # # # # # # # # # # # # # # . .
. . . . . . . # # # # # # # # # # # # # # # # .
. . . . . . . . # # # . # # # # # # # # # # # .
. . . . . . . . . . . . . # # # # # # # # # # .
. . . . . . . . . . . . . # # # # # # # # # # .
. . . . . . . . . . . . . . # # # # # # # # # .
. . . . . . . . . . . . . . # . # # # # # . . .
. . . . . . . . . . . . . . . . . # # . . . . .
. . . . . . . . . . . . . . . . . # # . . . . .
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . # . . . .
http://codepad.org/rNPrgQ3H

C# - 330
277 if you drop the using statement and remove the leading whitespace and line endings
int c,i,j,w=300,h=300,m=50;
using(var b=new Bitmap(w,h)){
for(i=0;i<w;i++){
for(j=0;j<h;j++){
double x0=4.0*(i-w/2)/w-1,y0=4.0*(j-h/2)/h,x=0.0,y=0.0;
for(c=0;x*x+y*y<=4.0&&c<m;c++){
var t=x*x-y*y+x0;
y=2.0*x*y+y0;x=t;
}
int v=c==m?255:c*10%255;
b.SetPixel(i,j,Color.FromArgb(v,v,v));
}
}
}
Not record setting by a long shot - but fun to convert from the Python sample source and compress down beyond readable.

Related

Reverse SHA-256 sigma0 function within complexity of O(n)?

Introduction
As a part of SHA-256 hashing algorithm, there's a function that is often being referred as σ1, or sigma0 for convenience. Basically, it takes X as input, where X is 32-bit unsigned value. Then converts it like this:
ROTATE_RIGHT(X, 7) ^ ROTATE_RIGHT(X, 18) ^ SHIFT_RIGHT(X, 3)
A bit of explanation, if you need one:
ROTATE_RIGHT(X, Y) - rotates X's bits to the right by Y
SHIFT_RIGHT(X, Y) - shifts X's bits to the right by Y, so the first Y bits of the result are always 0
Also, if you need the code, here's the full version in Python:
def rotate_right(x, y):
return (((x & 0xffffffff) >> (y & 31)) | (x << (32 - (y & 31)))) & 0xffffffff
def shift_right(x, n):
return (x & 0xffffffff) >> n
def sigma0(x):
return rotate_right(x, 7) ^ rotate_right(x, 18) ^ shift_right(x, 3)
Reverse function
I started wondering if that thing is reversible, and, to my surprise, it didn't take long to write a function which, by given sigma0's output, returns input of that function, or, simply put, reverses sigma0 function. I won't put the code here, because it was written in Node.js and modified a lot by more complex needs of searching particular sigma0 inputs by masks, but I'd like to give you a basic idea of how I solved it, so maybe you could enlighten me with some fresh ideas on how to achieve what I need.
My solution is simple, but it is also recursive. We know that every output's bit is the result of XOR operation of two or three input's bits. So I made a dependence table so that I can see how are output's bits are being affected by input ones:
I: 00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
R7 25,26,27,28,29,30,31,00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24
R18 14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,00,01,02,03,04,05,06,07,08,09,10,11,12,13
S3 zz,zz,zz,00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28
---------------------------------------------------------------------------------------------------
O: 00,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
What is this thing about? Say, in the output's 1st bit we have 1. For convenience, I'll write it as O[0], O[1], ... O[31], so O[x] is (x+1)th bit of the output. The same for input, marked as I.
So, O[0] == 1. In the table above we see that O[0] is the result of XOR operation of I[25] and I[14]. Which implies that one and only one of those input's bits must be 1. So at this point we could say that we can create two suitable masks for the input:
##############0#########1#######
##############1#########0#######
Those masks are key to the solution, at least, to mine. # means any value (0 or 1). When we create masks, we call the recursive function for the next bit, but preserving the mask. If we are out of possible masks that would suit previous mask, the previous mask has no solution, and if we reach 32nd bit, we're guaranteed to have no sharps in the masks, and this will be the answer.
First, I need to tell you that this thing works. But on Node.js it calculates every value for about 100ms and I have no idea what is the worst complexity of my recursion algorithm, because it's quite hard to measure. It doesn't satisfy me, and I broke my brains trying to solve this O(n).
Problem
I'm wondering if it's possible to write a function that reverses sigma0 within complexity of O(n), where n is amount of bits in the input/output and it equals to 32, without recursion, masks or trees, simply and fast.
I haven't concluded any mathematical proof for my statement, but I tested lots of different values and I can confidently claim that the amount of input values is equal to the amount of output values of this function, and both are equal to 2^32 - 1. In other words, for every output, there is one and only one possible input of sigma0 function.
Which gives me a thought that the fact sigma0 original function produces result with complexity of O(n) implies that the reverse function must have a solution that also works O(n).
If you mathematically prove me that this is impossible, I'd also accept this answer, but I haven't found anything that would indicate the impossibility of this task.
Resource-devouring workaround
In case I had free 16gb of ram, I'd be able to pre-calculate all the possible values into the file, and then load it into ram as a huge array. But it's not a solution since there are other 3 similar functions, and to do that for all of them I'd need 64gb of ram which is too expensive and excessive for this simple task.
UPD: Gaussian Elimination
Thanks to Artjom B.'s comment, I found a great way to solve XOR equations via Gaussian Elimination. Currently I'm trying to solve a matrix like this:
Input: 00000000100110101000111011101001
Output: 01110001101010000010010011100110
0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 | 0
1: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1
2: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 | 1
3: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 | 1
4: 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 | 0
5: 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 | 0
6: 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 | 0
7: 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 | 1
8: 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 | 1
9: 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 | 0
10: 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 | 1
11: 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 | 0
12: 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 | 1
13: 0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 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 | 0
28: 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 | 0
29: 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 | 1
30: 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 | 1
31: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 | 0
Published the matrix so you could see how it looks and not waste your time on creating it by yourself. I'll update my question once I solved it or not.
If we view sigma0 as a function over a GF(2)32 vector, you will note that it's linear. Addition in GF(2)32 is just the binary XOR:
>>> sigma0(235 ^ 352124)
2045075788
>>> sigma0(235) ^ sigma0(352124)
2045075788
This means that if we can find sigma0(x0) = 0b1, sigma0(x1) = 0b10, etc we can easily invert anything bit-by-bit. We can easily find these inverses with z3:
import z3
def z3_sigma0(x):
return z3.RotateRight(x, 7) ^ z3.RotateRight(x, 18) ^ z3.LShR(x, 3)
s = z3.Solver()
xs = [z3.BitVec(f"x{i}", 32) for i in range(32)]
for i in range(32):
s.add(z3_sigma0(xs[i]) == (1 << i))
print(s.check())
m = s.model()
for i in range(32):
print("x{:02} = 0x{:08x}".format(i, m[xs[i]].as_long()))
This instantly outputs:
sat
x00 = 0x185744e9
x01 = 0x30ae89d2
x02 = 0x615d13a4
x03 = 0xdaed63a1
x04 = 0x9cd03a8e
x05 = 0x08fdcc39
x06 = 0x11fb9872
x07 = 0x23f730e4
x08 = 0x5fb92521
x09 = 0xbf724a42
x10 = 0x57ee6948
x11 = 0xafdcd290
x12 = 0x76b358ec
x13 = 0xf531f531
x14 = 0xc36917ae
x15 = 0xb78f9679
x16 = 0x4615d13e
x17 = 0x947ce695
x18 = 0x19a4740f
x19 = 0x2b1facf7
x20 = 0x4e681d07
x21 = 0x84877ee7
x22 = 0x385344eb
x23 = 0x70a689d6
x24 = 0xf91a5745
x25 = 0xc36917af
x26 = 0xb78f967b
x27 = 0x4615d13a
x28 = 0x8c2ba274
x29 = 0x290afdcd
x30 = 0x4a42bf73
x31 = 0x94857ee6
Thus we can use this to make our inversion function:
sigma0_singleton_inverses = [
0x185744e9, 0x30ae89d2, 0x615d13a4, 0xdaed63a1, 0x9cd03a8e, 0x08fdcc39,
0x11fb9872, 0x23f730e4, 0x5fb92521, 0xbf724a42, 0x57ee6948, 0xafdcd290,
0x76b358ec, 0xf531f531, 0xc36917ae, 0xb78f9679, 0x4615d13e, 0x947ce695,
0x19a4740f, 0x2b1facf7, 0x4e681d07, 0x84877ee7, 0x385344eb, 0x70a689d6,
0xf91a5745, 0xc36917af, 0xb78f967b, 0x4615d13a, 0x8c2ba274, 0x290afdcd,
0x4a42bf73, 0x94857ee6
]
def inv_sigma0(x):
r = 0
for i in range(32):
if x & (1 << i):
r ^= sigma0_singleton_inverses[i]
return r
And indeed:
>>> def test_inv_once():
... r = random.randrange(2**32)
... return inv_sigma0(sigma0(r)) == r
>>> all(test_inv_once() for _ in range(10**6))
True
The above can be written completely loopless and branchless:
def inv_sigma0(x):
xn = ~x
r = (((xn >> 0) & 1) - 1) & 0x185744e9
r ^= (((xn >> 1) & 1) - 1) & 0x30ae89d2
r ^= (((xn >> 2) & 1) - 1) & 0x615d13a4
r ^= (((xn >> 3) & 1) - 1) & 0xdaed63a1
r ^= (((xn >> 4) & 1) - 1) & 0x9cd03a8e
r ^= (((xn >> 5) & 1) - 1) & 0x08fdcc39
r ^= (((xn >> 6) & 1) - 1) & 0x11fb9872
r ^= (((xn >> 7) & 1) - 1) & 0x23f730e4
r ^= (((xn >> 8) & 1) - 1) & 0x5fb92521
r ^= (((xn >> 9) & 1) - 1) & 0xbf724a42
r ^= (((xn >> 10) & 1) - 1) & 0x57ee6948
r ^= (((xn >> 11) & 1) - 1) & 0xafdcd290
r ^= (((xn >> 12) & 1) - 1) & 0x76b358ec
r ^= (((xn >> 13) & 1) - 1) & 0xf531f531
r ^= (((xn >> 14) & 1) - 1) & 0xc36917ae
r ^= (((xn >> 15) & 1) - 1) & 0xb78f9679
r ^= (((xn >> 16) & 1) - 1) & 0x4615d13e
r ^= (((xn >> 17) & 1) - 1) & 0x947ce695
r ^= (((xn >> 18) & 1) - 1) & 0x19a4740f
r ^= (((xn >> 19) & 1) - 1) & 0x2b1facf7
r ^= (((xn >> 20) & 1) - 1) & 0x4e681d07
r ^= (((xn >> 21) & 1) - 1) & 0x84877ee7
r ^= (((xn >> 22) & 1) - 1) & 0x385344eb
r ^= (((xn >> 23) & 1) - 1) & 0x70a689d6
r ^= (((xn >> 24) & 1) - 1) & 0xf91a5745
r ^= (((xn >> 25) & 1) - 1) & 0xc36917af
r ^= (((xn >> 26) & 1) - 1) & 0xb78f967b
r ^= (((xn >> 27) & 1) - 1) & 0x4615d13a
r ^= (((xn >> 28) & 1) - 1) & 0x8c2ba274
r ^= (((xn >> 29) & 1) - 1) & 0x290afdcd
r ^= (((xn >> 30) & 1) - 1) & 0x4a42bf73
r ^= (((xn >> 31) & 1) - 1) & 0x94857ee6
return r
The fastest version probably probably is this one, grouping by 16 bits at a time using a 2 × 216 size lookup table (or similarly four lookups into a 4 × 28 sized table).
sigma0_16bit_inverse_lo = [inv_sigma0(x) for x in range(2**16)]
sigma0_16bit_inverse_hi = [inv_sigma0(x << 16) for x in range(2**16)]
def fast_inv_sigma0(x):
return (sigma0_16bit_inverse_lo[x & 0xffff] ^
sigma0_16bit_inverse_hi[(x >> 16) & 0xffff])

Is there a way to show the index of atoms in rdkit.Chem.rdmolops.GetAdjacencyMatrix?

I'm trying to convert a compound from mol to adjacency matrix. However, i encountered a problem that rdkit.Chem.rdmolops.GetAdjacencyMatrix() doesn't provide the index of the atoms for the adjacency matrix. Is there any way to include the index data for the adjacency matrix in rdkit?
rdkit.Chem.rdmolops.GetAdjacencyMatrix((Mol)mol)
As the RDKit AdjacencyMatrix is ordered from zero upwards, you can convert it to a Pandas dataframe.
from rdkit import Chem
import pandas as pd
s = 'CCC(C(O)C)CN'
mol = Chem.MolFromSmiles(s)
am = Chem.GetAdjacencyMatrix(mol)
print(am)
[[0 1 0 0 0 0 0 0]
[1 0 1 0 0 0 0 0]
[0 1 0 1 0 0 1 0]
[0 0 1 0 1 1 0 0]
[0 0 0 1 0 0 0 0]
[0 0 0 1 0 0 0 0]
[0 0 1 0 0 0 0 1]
[0 0 0 0 0 0 1 0]]
df = pd.DataFrame(am)
print(df)
0 1 2 3 4 5 6 7
0 0 1 0 0 0 0 0 0
1 1 0 1 0 0 0 0 0
2 0 1 0 1 0 0 1 0
3 0 0 1 0 1 1 0 0
4 0 0 0 1 0 0 0 0
5 0 0 0 1 0 0 0 0
6 0 0 1 0 0 0 0 1
7 0 0 0 0 0 0 1 0
If you want elements instead of indices
element = [atom.GetSymbol() for atom in mol.GetAtoms()]
print(element)
['C', 'C', 'C', 'C', 'O', 'C', 'C', 'N']
df_e = pd.DataFrame(am, index=element, columns=element)
print(df_e)
C C C C O C C N
C 0 1 0 0 0 0 0 0
C 1 0 1 0 0 0 0 0
C 0 1 0 1 0 0 1 0
C 0 0 1 0 1 1 0 0
O 0 0 0 1 0 0 0 0
C 0 0 0 1 0 0 0 0
C 0 0 1 0 0 0 0 1
N 0 0 0 0 0 0 1 0

unix, merging several text file contents, horizontally (problems with paste)

ok, let's I have a txt file like this...
X 1 : D i s t a n c e [ m m ]
Y 1 : I n t e n s i t y
X 2 : D i s t a n c e [ m m ]
Y 2 : I n t e n s i t y
I m a g e ( 2 3 7 . 2 3 u )
X 1 Y 1
0 . 0 0 0 0 0 0 4 0 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 5 7 . 0 0 0 0 0 0
...etc
And several others similar to this...
X 1 : D i s t a n c e [ m m ]
Y 1 : I n t e n s i t y
X 2 : D i s t a n c e [ m m ]
Y 2 : I n t e n s i t y
I m a g e ( 2 6 5 . 2 7 u )
X 1 Y 1
0 . 0 0 0 0 0 0 3 6 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 3 4 . 0 0 0 0 0 0
0 . 0 0 4 0 0 0 4 0 . 0 0 0 0 0 0
When I use paste, to merge horizontally the content of these files...
#! /bin/bash
zeta=$(ls)
paste $zeta >> file_1.txt
I get this (example if there were two files):
X 1 : D i s t a n c e [ m m ]
X 1 : D i s t a n c e [ m m ]
Y 1 : I n t e n s i t y
Y 1 : I n t e n s i t y
X 2 : D i s t a n c e [ m m ]
X 2 : D i s t a n c e [ m m ]
Y 2 : I n t e n s i t y
Y 2 : I n t e n s i t y
I m a g e ( 2 3 7 . 2 3 u )
I m a g e ( 2 6 5 . 2 7 u )
X 1 Y 1
X 1 Y 1
0 . 0 0 0 0 0 0 4 0 . 0 0 0 0 0 0
0 . 0 0 0 0 0 0 3 6 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 5 7 . 0 0 0 0 0 0
0 . 0 0 2 0 0 0 3 4 . 0 0 0 0 0 0
0 . 0 0 4 0 0 0 4 1 . 0 0 0 0 0 0
0 . 0 0 4 0 0 0 4 0 . 0 0 0 0 0 0
Why do I have this intermingle of lines?
How can I do to put exactly the content of a txt file just aside of the content of the other txt file? In this case have the columns 1 and 2 for my first file, and the columns 3 and 4 for my second file. And then massively for several files?
Thanks for any hint,
Maybe you can put several '\t' between context of the line and '\n' :
cat text1.txt | tr "\n" "\t\t\n" > text1.txt
After the processes, you can use your old method to paste them together. :)

What is "minlex order" when representing a sudoku puzzle?

For a sudoku puzzle:
000000010400000000020000000000050407008000300001090000300400200050100000000806000
in it's minlex order form:
000000001000000020000003000000040500006000300007810000010020004030000070950000000
What is the meaning of minlex order and how the above puzzle is drawn in a 9x9 layout?
Source: Minimum Sudoku 1st puzzle.
UPDATE:
Diagram 1:
0 0 0 0 0 0 0 1 0
4 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0
0 0 0 0 5 0 4 0 7
0 0 8 0 0 0 3 0 0
0 0 1 0 9 0 0 0 0
3 0 0 4 0 0 2 0 0
0 5 0 1 0 0 0 0 0
0 0 0 8 0 6 0 0 0
Diagram 2:
0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 2 0
0 0 0 0 0 3 0 0 0
0 0 0 0 4 0 5 0 0
0 0 6 0 0 0 3 0 0
0 0 7 8 1 0 0 0 0
0 1 0 0 2 0 0 0 4
0 3 0 0 0 0 0 7 0
9 5 0 0 0 0 0 0 0
The puzzle is drawn from the specification by writing each number in a grid space, starting from the top left, going across the top row, and then starting on the second row from left to right until all nine rows are filled in. A zero corresponds to a blank space.
Like this:
1 2 3 4 5 6 7 8 9
+---------------+---------------+---------------+
A | . . . | . . . | . 1 . |
B | 4 . . | . . . | . . . |
C | . 2 . | . . . | . . . |
|---------------+---------------+---------------|
D | . . . | . 5 . | 4 . 7 |
E | . . 8 | . . . | 3 . . |
F | . . 1 | . 9 . | . . . |
|---------------+---------------+---------------|
G | 3 . . | 4 . . | 2 . . |
H | . 5 . | 1 . . | . . . |
I | . . . | 8 . 6 | . . . |
+---------------+---------------+---------------+
The minlex form of a Sudoku puzzle is the equivalent puzzle that sorts first in alphabetical order (from left to right). Puzzles are considered equivalent if one can be converted to the other by interchanging row and columns and renumbering without changing the puzzle.

how to convert these 7-segment decoder to boolean expression

how to convert these 7-segment decoder to boolean expression??
BCD 7-Segment decoder
A B C D a b c d e f g
0 0 0 0 0 0 0 0 0 0 1
0 0 0 1 1 0 0 1 1 1 1
0 0 1 0 0 0 1 0 0 1 0
0 0 1 1 0 0 0 0 1 1 0
0 1 0 0 1 0 0 1 1 0 0
0 1 0 1 0 1 0 0 1 0 0
0 1 1 0 0 1 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 1 0 0
I suggest you use a karnough map.
You'll need to use one for each result column, so 7 4x4 tables.
There are even a few karough map generators on the web that you can use.

Resources