The post The Even-more Complete List of Hex Words appeared first on Stoney's Zone.

]]>

A 1 AC AD A5 A7 BC BE B1 CD C5 DB DC D0 ED E5 E7 FA 1D 1F 15 17 0F 01

02 50 7A 7B 71 70 75 ABC ABE AB5 ACE AC7 ADA ADD AD0 AD5 AD2 AF7 A1D A55

A7E A27 BAA BAD BA7 BBC BED BEE BE7 B1B B1C B1D B15 B17 B12 B0A B0B B0D

B00 CAB CAD CA7 CB5 CE0 CFC C1A C1D C0B C0D C00 C07 DAB DAD DD7 DEB DEE

D1D D1E D15 D0A D0B D0C D0E D05 D07 EA7 EBB EC0 EDE EFF E7A FAB FAD FA7

FB1 FDA FED FEE FE2 F1B F1E F17 F0B F0E 1B0 1CE 1DA 1D5 1F5 15A 17D 170

175 0AF 0A7 0B1 0DD 0DE 0FF 0F7 5AC 5AD 5A7 5EA 5EE 5E7 51C 51D 515 517

50B 50D 505 507 7AB 7AD 7A0 7A7 7EA 7ED 7EE 7E7 71A 71C 71E 70D 70E 700

707 2ED 217 20E 200 ABBA ABBE ABED ABE7 ACED ACE5 AC1D AC75 ADD5 A1DA

A1DE A1D5 A51A A7E5 BAA5 BABE BADE BAE2 BA17 BA5E BA55 BA57 BA7E BA75

BEAD BEA7 BEDE BED5 BEEF BEE5 BEE7 BE55 BE57 BE7A BE75 B1A5 B1B5 B1DE

B1D5 B1FF B105 B17E B175 B0A5 B0A7 B0B5 B0DE B0D5 B00B B005 B007 B05E

B055 B075 B020 CAB5 CAD5 CAFE CAFF CA5E CA57 CA70 CA75 CA77 CECA CEDE

C1A0 C17E C175 C0A7 C0BB C0B5 C0CA C0C0 C0DA C0DE C0D5 C0ED C01F C005

C007 C057 C07E C075 DAB5 DACE DADA DAD0 DAD5 DAF7 DA15 DA7A DA7E DA2E

DEAD DEAF DEB5 DEB7 DEC0 DEED DEF7 D1A5 D1B5 D1CE D1D0 D1ED D1E5 D1E7

D15C D172 D0B5 D0C5 D0D0 D0E5 D0FF D05E D055 D057 D07E D075 D02E EA5E

EA57 EA75 EBB5 EDDA ED17 EFF5 E7A5 E77A FACE FAC7 FADE FAD5 FAFF FA57

FA7E FA75 FA2E FEA7 FED5 FEED FEE5 FEE7 FE55 FE57 FE7A FE7E F1A7 F1B5

F1D0 F1EF F1E5 F1FE F157 F175 F122 F0B5 F0E5 F00D F007 1B15 1CED 1CE5

1DEA 1DE5 107A 1515 120D 0AF5 0A75 0B1E 0B15 0B17 0B0E 0DD5 0DE5 0D15

0FF5 015E 002E 0715 0770 5AAB 5ABA 5AC5 5ADE 5AFE 5A1D 5A55 5A7E 5CAB

5CAD 5CA7 5C07 5EA5 5EA7 5EC5 5EC7 5EED 5EE5 5E75 5E77 51C5 51DE 51F7

517E 5175 512E 50B5 50C5 50DA 50D5 50FA 50F7 5007 505A 5070 5075 57AB

57E7 7AB5 7AC0 7AC7 7AD5 7AF7 7A55 7A7E 7A75 7EA5 7EA7 7ED5 7EED 7EE5

7E55 7E57 71C5 71DE 71ED 71E5 71FF 7170 7122 70AD 70DD 70ED 70E5 70FF

7007 7055 707E 7070 7075 2ED5 2E57 2E7A 21B0 2175 2005 ABAC1 ABAF7 ABA5E

ABA7E ABBA5 ABBE5 ABB07 ABE75 AB1DE AB0DE AC1D5 AC7ED ADDED ADD1E AD105

AD0BE AD2E5 AF007 A1DED A1DE5 A5C11 A5C07 A51DE A55AD A55E5 A55E7 A771C

A27EC BAAED BABE5 BA175 BA12E BA5ED BA5E5 BA51C BA51E BA515 BA550 BA57E

BA575 BA7ED BA7E5 BEAD5 BEA57 BEA75 BEEBE BEEF5 BEE75 BEF17 BE5E7 BE507

BE575 BE7A5 BE77E BE771 B1DE5 B1DE7 B1FF5 B107A B17E5 B12E7 B0A57 B0A75

B0BB1 B0DED B0DE5 B0FF0 B015E B00B5 B00ED B0057 B0075 B002E B0205 CAB07

CACA0 CAC71 CADE7 CAD12 CAFE5 CAFF5 CA5ED CA5E5 CA510 CA57E CA575 CEA5E

CEDED CEDE5 C1A05 C17ED C17E5 C0A57 C0A75 C0CC1 C0C0A C0C05 C0DA5 C0DED

C0DE5 C0ED5 C01F5 C00ED C0075 C05ED C05E5 C0575 C07E5 DACE5 DAC1A DA7ED

DA7E5 DA2ED DA2E5 DEB17 DEB75 DECAF DECCA DEED5 DEF0E DE1CE DE157 D1CED

D1CE5 D1C7A D1D57 D1E75 D10DE D15C0 D15C5 D1770 D0D05 D0E57 D0FF5 D05ED

D05E5 D07ED D07E5 D02ED D02E5 EA5ED EA5E5 EA575 EBBED EDD1E ED1C7 ED175

EFFED EFF1E E551E E57E5 FACED FACE5 FACE7 FAC75 FADED FADE5 FAFF5 FA575

FA7ED FA7E5 FA2ED FA2E5 FEA57 FEA75 FECE5 FEED5 FE575 FE7ED FE7E5 FE71D

F1A75 F1CE5 F1EF5 F1FE5 F1575 F0157 F00D5 F0075 1B12A 1DEA5 1D107 107A5

15AAC 155AC 0A5E5 0A515 0A7E5 0BE5E 0B1ED 0B175 0B0E5 0C7E7 0DE75 0FFED

002ED 002E5 0221E 5AB07 5ACC0 5ADA7 5ADE5 5AD1E 5AFE5 5A7ED 5A7E5 5CAB5

5CAD5 5CA75 5C0FF 5C007 5C075 5C077 5EA75 5EC75 5EED5 5E12E 5E775 51DED

51DE5 51F75 515E5 517ED 517E5 512ED 512E5 50DA5 50FA5 50F1A 57AB5 57AC1

57AFF 57A1D 57A7E 57A75 57EAD 57EED 57E75 571E5 571FF 570A7 5701C 5700D

7AB00 7AC17 7AC05 7A57E 7EAED 7EA5E 7EA75 7E55A 7E575 71BE7 71B1A 71DED

71DE5 71FF5 70AD5 70A57 70B17 70FF5 70075 705CA 707ED 707E5 2E575 2E7A5

ABA5ED ABA5E5 ABA7ED ABA7E5 ABBE55 ABB075 ABB077 AB1DE5 AB0DED AB0DE5

ACAC1A ACAD1A ACCEDE ACCE55 ACC057 ACE71C AC1D1C AC057A ADD1C7 AD1DA5

AD0BE5 AFFEC7 A5C075 A51DE5 A55E55 A55E75 A55151 A55157 A5512E A77E57

A771CA A771C5 A27EC5 BAB1ED BAB1E5 BADD1E BA17ED BA0BAB BA5E57 BA51C5

BA55E5 BA55E7 BA5505 BA57ED BA57E5 BA77ED BEADED BEA575 BEDDED BED517

BEEFED BEF175 BE5E75 BE51DE BE5075 BE551E BE57ED BE71DE BE771E B1A5ED

B1A5E5 B1DE75 B1FFED B1071C B15EC7 B0A575 B0A7ED B0BBED B0BB1E B0BCA7

B0CC1E B0D1CE B0D1ED B0D1E5 B0FF05 B00BED B00575 B007ED B007EE B007E5

B002ED B002E5 B055ED B055E5 CABBED CACA05 CADE75 CA5ABA CA551A CA551E

CA57ED CA57E5 CEA5ED CEA5E5 C1CADA C171E5 C0A575 C0A7ED C0BBED C0CC15

C0C0A5 C0DDED C0FFEE C0071E C055E7 C057C0 C077ED C021E5 DABBED DAD0E5

DA15E5 DEBA5E DEBA7E DEBB1E DEB175 DECADE DECAFF DECAF5 DECE17 DEC1DE

DEC0DE DEEDED DEFACE DEFEA7 DEFEC7 DEF1ED DEF1E5 DE1CED DE1CE5 DE1575

DE5157 DE7EC7 DE7E57 D1D0E5 D1E7ED D10DE5 D15C05 D155ED D155E5 D17705

D172E5 D0BBED D0FFED D00DAD D055ED D077ED EA51E5 EDD1ED EDD1E5 ED1C75

ED17ED EFFACE EFFEC7 EFFE7E E57A7E FACADE FACE75 FAFFED FA5C1A FA57ED

FA77ED FEA575 FE55ED FE55E5 FE22E5 F1A5C0 F1BBED F1E57A F177ED F122ED

F122E5 F0BBED F0E71D F01575 F00D1E F007ED F0071E 1B15E5 1C1E57 1D1075

10D1DE 10D12E 17A5CA 0B0157 0B5E55 0C7E75 0DDE57 0DE55A 0FF1CE 0FF5E7

0F7E57 07105E 5AB075 5AD157 5AFE57 5A55ED 5A55E5 5C0FF5 5C0075 5EABED

5EA7ED 5ECEDE 5EDA7E 5EEDED 5E12ED 5E12E5 5E77EE 51E57A 51F7ED 50BBED

50DDED 57AC1E 57AFF5 57A515 57A7ED 57A7E5 57A71C 57EAD5 57EED5 571FF5

570A75 5701C5 7ABBED 7AB005 7AC71C 7A0157 7A57ED 7A57E5 7A77ED 7A771E

7A7700 7EA5ED 7EA5E5 7E551E 7E57ED 7E57E5 7E5715 71B1AE 71DB17 71D1ED

71D1E5 71FFED 70A575 70FFEE 7007ED 7055ED 7055E5 7077ED 75E75E 20D1AC

ABBA51D ABE77ED AB5CE55 ACAC1A5 ACCEDED ACCEDE5 ACC0575 ACE7A7E ADD1C75

AFFEC75 A5CE71C A51A71C A551575 A5512ED A5512E5 A77E575 BABB177 BAB1E57

BADDE57 BADD1E5 BA0BAB5 BA55E75 BA55157 BA7157A BA7157E BED51DE BED5175

BE51DE5 BE71DED BE71DE5 B1DD1E5 B15EC75 B0A57ED B0BB1E5 B0BB177 B0BCA75

B0D1CE5 B0E071A B00B1E5 B0057ED B007EE5 B0071E5 CAB005E CADD1ED CADD1E5

CADE77E CA171FF CA5ABA5 CA5CADE CA55A77 CA551A5 CA570FF CA7B0A7 C1CADA5

C1551E5 C15501D C0A57ED C0D1CE5 C0FFEE5 C01FFED C0071E5 C055E75 C021E57

DADA157 DADD1E5 DAF7E57 DA151E5 DEADE57 DEAFE57 DEBA5ED DEBA5E5 DEBA7ED

DEBA7E5 DEB17ED DECADE5 DECAFF5 DECEA5E DECE175 DEC1DED DEC1DE5 DEC0DED

DEC0DE5 DEFACED DEFACE5 DEFEA75 DEFEC75 DEFFE57 DEF1C17 DEF7E57 DE1C71C

DE1F1ED DE1F1E5 DE1571C DE171E5 DE51575 DE7EC75 DE7E575 D1C1E57 D1C7A7E

D10CE5E D15C0ED D15EA5E D155EC7 D157AFF D1771E5 D1770ED D1221ED D1221E5

D00DAD5 D021E57 EA51E57 EC0C1DE ED1F1CE ED1F1ED ED1F1E5 EFFACED EFFACE5

EFFEC75 E1DE71C E57A7E5 FACADE5 FACE7ED FAC701D FADD157 FA5C1A5 FA5C157

FA57E57 FA77E57 FA771E5 FEA57ED F1E57A5 F1F71E5 F177E57 F0157ED F00D1E5

F00751E 1ACC0CA 1CEB0A7 1D1071C 1FF1E57 10D1DE5 10D12ED 10D12E5 0B01575

0FFBEA7 0FF1CE5 0FF5E75 0FF51DE 0021E57 5ADDE57 5AD1575 5A71A7E 5CABBED

5CAB1E5 5CA77ED 5C1A71C 5C0FFED 5C007ED 5C0771E 5EABED5 5EAF00D 5EA51DE

5ECEDED 5ECEDE5 5EDA7ED 5EDA7E5 5EEDBED 5E77EE5 51E57A5 51551E5 50F7E57

50F71E5 57ABBED 57AFFED 57A71C5 57E77ED 571FFED 7ABA5C0 7ABB1E5 7AB00ED

7AC71C5 7AFFE7A 7AFF1E5 7A01575 7A771E5 7A77005 7EDD1E5 7E57A7E 7E57BED

71DB175 71D1E57 71771E5 71221E5 70AD1ED 70AD1E5 70A57ED 70BACC0 70CCA7A

70DD1E5 70FFEE5 700751E 75E75E5 2EBEDEE 20D1AC5 ABBE55E5 ABD1CA7E

AB5C155A ACCE55ED ACCE55E5 ACC057ED ACE7A7E5 AC1D0515 ADD1C7ED AFFEC7ED

A5BE5705 A5CE71C5 A51A71C5 A55E55ED A55E55E5 A55157ED A77E57ED BA551575

BA771E57 BEAD1E57 BEA71F1C BED51DE5 BED57EAD BEEF1E57 BEF177ED BE5077ED

B15EC7ED B1771E57 B0AD1CEA B0021E57 B0551E57 CAB005E5 CADE77E5 CA171FF5

CA5CADED CA5CADE5 CA55E77E CA570FF5 CA7B0A75 CA771E57 C15501D5 C171F1ED

C0A77E57 C0D1F1ED C0D1F1E5 C055E7ED DADA1575 DAFF1E57 DA7ABA5E DEADBEA7

DECEA5ED DECEA5E5 DED1CA7E DEFEA7ED DEFECA7E DEFEC7ED DEF1C175 DE5157ED

DE7EC7ED DE7E57ED D1ABE7E5 D1ABE71C D1A57A5E D1C7A7ED D1C7A7E5 D1DAC71C

D1E7E71C D10CE5E5 D15EA5ED D15EA5E5 D155EC75 D157AFF5 D157A57E D1751E57

D1221E57 D0771E57 EC57A71C ED1F1CE5 EFFEC7ED FAC701D5 FADD1575 FA5C1575

FA771E57 F1A5C0E5 F1221E57 F00751E5 1CEB0A75 1D10C1E5 0B5E55ED 0B5E55E5

0DD171E5 0FFBEA75 0FF51DE5 0551F1ED 0551F1E5 5AD1571C 5AFE71ED 5AFE71E5

5A551E57 5A71A7ED 5A71A7E5 5C1A71CA 5C0771E5 5EAC0A57 5EA51DE5 5EDA7E57

5EEDBED5 5EEDCA5E 5EED1E57 51551E57 50071E57 57ACCA70 57A1DE57 57EAD1ED

57EAD1E5 571FFE57 7ABA5C05 7A571E57 7A771E57 7A7700ED 7E57A7E5 7E57BED5

7E571E57 7171CACA 70A571E5 70BACC05 70CCA7A5 700751E5 2E571E57 ABD1CA7ED

ABD1CA7E5 AB5CE55ED AB5CE55E5 AB5C155AE AB5C155A5 AC1D1F1ED AC1D1F1E5

AD1ABA71C A550C1A7E BEA71F1ED BEA71F1E5 BED57EAD5 B0CCACC10 CA55E77E5

C055E77ED DA7ABA5ED DA7ABA5E5 DEADBEA75 DED1CA7ED DED1CA7E5 DEFAECA7E

DEFEA7157 DEFECA7ED DEFECA7E5 DE51CCA7E D1ABE71C5 D1DAC71C5 D1E7E71C5

D15AFFEC7 D155EC7ED D157A57E5 EC57A51E5 FA5C1571C FE1571E57 15057A71C

0FF1C1A7E 5A715F1ED 5A715F1E5 5CABB1E57 5EAC0A575 5EEDCA5E5 51551F1ED

50C1E71E5 57ACCA705 57A7E51DE 57A71571C 57EADFA57 57EAD1E57 7A7700157

7E571F1ED 7E571F1E5 70A571E57 A5BE570515 A550C1A7ED A550C1A7E5

DEFEA71575 DE51CCA7ED DE51CCA7E5 DE551CA7ED D15AFFEC75 D1550C1A7E

E157EDDF0D 0FF1C1A7ED 0FF1C1A7E5 57A71571C5 7A77001575 D15AFFEC7ED

D1550C1A7ED D1550C1A7E5 E157EDDF0D5 D15A550C1A7E D155A715F1ED

D155A715F1E5 D15A550C1A7ED D15A550C1A7E5

The post The Even-more Complete List of Hex Words appeared first on Stoney's Zone.

]]>The post The Complete List of Hex Words appeared first on Stoney's Zone.

]]>

Everyone knows 0xDEADBEEF and 0xCAFEBABE, but I wondered what else was available, so I wrote a short Lisp program to run through the standard Unix English dictionary and extract the usable words. I’ve included substituting 1 for I and 0 for O. There aren’t that many words, so here they are, sorted by length and alphabetically:

A 1 AC AD BC BE B1 CD DB DC D0 ED FA 1D 1F 0F 01 ABC ABE ACE ADA ADD AD0

A1D BAA BAD BBC BED BEE B1B B1C B1D B0A B0B B0D B00 CAB CAD CE0 CFC C1A

C1D C0B C0D C00 DAB DAD DEB DEE D1D D1E D0A D0B D0C D0E EBB EC0 EDE EFF

FAB FAD FB1 FDA FED FEE F1B F1E F0B F0E 1B0 1CE 1DA 0AF 0B1 0DD 0DE 0FF

ABBA ABBE ABED ACED AC1D A1DA A1DE BABE BADE BEAD BEDE BEEF B1DE B1FF

B0DE B00B CAFE CAFF CECA CEDE C1A0 C0BB C0CA C0C0 C0DA C0DE C0ED C01F

DACE DADA DAD0 DEAD DEAF DEC0 DEED D1CE D1D0 D1ED D0D0 D0FF EDDA FACE

FADE FAFF FEED F1D0 F1EF F1FE F00D 1CED 1DEA 0B1E 0B0E ABAC1 AB1DE AB0DE

ADDED ADD1E AD0BE A1DED BAAED BEEBE B0BB1 B0DED B0FF0 B00ED CACA0 CEDED

C0CC1 C0C0A C0DED C00ED DAC1A DECAF DECCA DEF0E DE1CE D1CED D10DE EBBED

EDD1E EFFED EFF1E FACED FADED 0B1ED 0FFED AB0DED ACAC1A ACAD1A ACCEDE

AC1D1C BAB1ED BADD1E BA0BAB BEADED BEDDED BEEFED B1FFED B0BBED B0BB1E

B0CC1E B0D1CE B0D1ED B00BED CABBED C1CADA C0BBED C0DDED C0FFEE DABBED

DEBB1E DECADE DECAFF DEC1DE DEC0DE DEEDED DEFACE DEF1ED DE1CED D0BBED

D0FFED D00DAD EDD1ED EFFACE FACADE FAFFED F1BBED F0BBED F00D1E 10D1DE

0FF1CE ACCEDED CADD1ED C01FFED DEC1DED DEC0DED DEFACED DE1F1ED EC0C1DE

ED1F1CE ED1F1ED EFFACED 1ACC0CA B0AD1CEA C0D1F1ED AC1D1F1ED B0CCACC10

The post The Complete List of Hex Words appeared first on Stoney's Zone.

]]>The post 2D Matrix Decomposition appeared first on Stoney's Zone.

]]>

I found that the formula for the polar decomposition, on page 3, produces rotation matrices that don’t account for diagonal axis flipping (swapping x with y). The article seems to factor out the flipping, but I didn’t want to have a fourth component. Removing the sign(det(M)) factor eliminated this problem by incorporating the flipping into the rotation matrix.

It’s very desirable to decompose a matrix generated by {angle, scale, shear} back into the same part values, but this happens only when scale is positive and shear is zero. If not, the rotation is affected. At least, the parts are stable once derived from the first matrix. This may help in UI, and certainly in interpolation.

This page is a Mathematica-generated HTML file, with CSS that I tweaked. Unfortunately, it shows Mathematica code as images, to preserve the original appearance. The original Mathematica notebook is here. If you don’t have Mathematica, you can view it using the free Mathematica Player.

The post 2D Matrix Decomposition appeared first on Stoney's Zone.

]]>The post Mirroring git respositories to DreamHost appeared first on Stoney's Zone.

]]>

I already have a lot of local git repositories, and this seemed like a much better way to back them up off-site than using github or similar. This is useful only for repositories without collaborators, of course.

Here’s the shell script. I used Bash just because that’s my normal shell.

All disclaimers disclaimed. This is not bulletproof.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/bin/bash # # Create a mirror of a local git repos on DreamHost. # Run this in the root directory of an existing git repository. # Note that this sets the "origin" remote. # # Derived from http://casperfabricius.com/site/2008/09/21/keeping-git-repositories-on-dreamhost-using-ssh/ # # change this to your ssh login. DREAMGIT_DOMAIN=user@domain.com # ensure that we're at the top of a git repos if [ ! -d .git ]; then echo "The current directory is not a git repository" exit 1 fi # get our directory name and split off the project name dirPath=`pwd` baseProjectName=${dirPath##/*/} # replace spaces with underscores in the project name projectName=${baseProjectName// /_} # remove any other troublesome characters projectName=${projectName//[^-a-zA-Z0-9_.]/} # make the remote mirror on DreamHost ssh $DREAMGIT_DOMAIN 'mkdir -p ~/git/'$projectName'.git && cd ~/git/'$projectName'.git && git --bare init' # set the remote as the default push and pull target git remote add origin --mirror ssh://$DREAMGIT_DOMAIN/~/git/$projectName.git # push all to it echo "Pushing everything to remote..." if git push -v origin; then echo "Git mirror at '$DREAMGIT_DOMAIN/git/$projectName.git' all set" else echo "git push failed" exit 1 fi |

I imagine that this will work for any server to which you can ssh.

The post Mirroring git respositories to DreamHost appeared first on Stoney's Zone.

]]>The post Defining new format wrappers in Mathematica appeared first on Stoney's Zone.

]]>

For example:

In[1]:= **-a b // CForm**

Out[1]//CForm= -(a*b)

That looks ugly. I tried to define a new format for Times[-1, v], but this requires unprotecting Times, and led to an infinite recursion when I tried to use it. I finally found a suggestion on how to define whole new format wrappers on the comp.soft-sys.math.mathematica list, in a message posted by Carl Woll (a Wolfram employee). This is how it works:

This assignment marks CCForm (my new formatter) as a format wrapper.

In[2]:= **Format[CCForm[expr_], CCForm] := expr**

Note that the Out[3] text shows the new formatter name.

In[3]:= **2 + 3 // CCForm**

Out[3]//CCForm= CCForm[5]

It’s showing the CCForm wrapper, though, which we don’t want to look at, so we define a top-level format for CCForm that replaces it with its argument.

In[4]:= **Format[CCForm[expr_]] := expr**

Now, the output looks correct.

In[5]:= **2 + 3 // CCForm**

Out[5]//CCForm= 5

The case I’m trying to fix isn’t helped by this. We need to make it produce the CForm.

In[6]:= **-a b // CCForm**

Out[6]//CCForm= -a b

In[7]:= **Format[CCForm[expr_]] := CForm[expr]**

In[8]:= **-a b // CCForm**

Out[8]//CCForm= -(a*b)

We don’t want the result to be evaluated, so we add a HoldForm.

In[9]:= **Format[CCForm[expr_]] := HoldForm[CForm[expr]]**

In[10]:= **-a b // CCForm**

Out[10]//CCForm= -(a*b)

Now it’s in CForm, but we still have the parenthesis. I don’t understand just why this works, but making CCForm HoldAll fixes the problem.

In[11]:= **SetAttributes[CCForm, HoldAll]**

In[12]:= **-a b // CCForm**

Out[12]//CCForm= -a * b

This also prevents evaluation of the expression being formatted.

In[13]:= **2 + 3 // CCForm**

Out[13]//CCForm= 2 + 3

Note that CForm does what I want if it’s made HoldAll.

In[14]:= **-a b // CForm**

Out[14]//CForm= -(a*b)

In[15]:= **SetAttributes[CForm, HoldAll]**

In[16]:= **-a b // CForm**

Out[16]//CForm= -a*b

Although it’s not styled as nicely as the CCForm version, and could mess up existing code, so we won’t do that.

In[17]:= **ClearAttributes[CForm, HoldAll]**

Now I can use CCForm instead of CForm for the output formatting of my converter, and have nicer-looking results.

The post Defining new format wrappers in Mathematica appeared first on Stoney's Zone.

]]>The post Converting symbolic Mathematica expressions to C code appeared first on Stoney's Zone.

]]>

After spending some time manually rearranging a closed-form solution to a system of equations, it seemed worthwhile to find a way to hoist common subexpressions into variables. I didn’t find anything useful via Google, as the results were swamped by references to the common compiler “common subexpression elimination” optimization. It looked like it would require data-flow analysis to do this, which would be tedious to write and debug.

Some thought and experimentation suggested that it wouldn’t be all that hard to do a decent job using a straight-forward pattern matching and replacement (especially since I didn’t care how fast this ran), so I created this Mathematica 7 notebook to do just that. The code turned out to be concise, which means “unreadable” in Mathematica. I try to stick to functional style where I can, which is nice mental exercise, but can be hard to read. In the main function, I opted for iteration rather than a purely functional recursion just because it seemed clearer.

It’s always interesting to work on a problem like this in Mathematica. The language is very expressive, but often fails at the “Principle of Least Surprise“. In this case, I kept being tripped up by Mathematica matching non-existant arithmetic functions (Plus, Times, etc.) everywhere in an expression, in an overly-helpful attempt to match arithmetic patterns regardless of commutative or associative expression rearrangements. For future reference, it turns out that using HoldPattern will prevent that behavior.

I created a web page from the Mathematica notebook, but there doesn’t appear to be any way to do that and produce text, rather than pictures of the code. Oh well. Here it is anyway. If you want to see this notebook properly and don’t have Mathematica 7, you can use the free Mathematica Player.

The post Converting symbolic Mathematica expressions to C code appeared first on Stoney's Zone.

]]>The post Adding a keyboard shortcut to Mathematica V7 appeared first on Stoney's Zone.

]]>In Mathematica, typing index brackets is clumsy, as one has to type esc-[[-esc then esc-]]-esc. I found a way to modify the Edit menu to add a shortcut for this.

I don’t know how this would be done from non-Macintosh systems, but on Macintosh, you copy the file “/Applications/Mathematica.app/SystemFiles/FrontEnd/TextResources/Macintosh/MenuSetup.tr” (in the Application bundle) into “~/Library/Mathematica/SystemFiles/FrontEnd/TextResources/Macintosh/MenuSetup.tr”. Then, edit it using a plain text editor like BBEdit, or even with Mathematica itself.

In this file, find the line

MenuItem["Matching []", "InsertMatchingBrackets", MenuKey["]", Modifiers->{"Command", "Option"}]], |

In the same block of menu items, add this

MenuItem["Matching [LeftDoubleBracket][RightDoubleBracket]", FrontEndExecute[{ FrontEnd`NotebookApply[FrontEnd`InputNotebook[], "[LeftDoubleBracket][SelectionPlaceholder][RightDoubleBracket]"]}], MenuKey["]", Modifiers->{"Command", "Option", "Control"}]], |

You have to restart Mathematica for it to see this change, but now you can wrap index brackets around the selection by typing cmd-opt-ctl-].

This is different from the other “Matching…” insertions, as it wraps the brackets around the selection, or leaves a “placeholder” selected if there was no selection rather than replacing the selection with the insertion. Since I’m used to editors that wrap the selection in this case, I’ve also changed the existing “Matching…” menu items to use this same technique.

This is what I have now:

MenuItem["Matching []", FrontEndExecute[{ FrontEnd`NotebookApply[ FrontEnd`InputNotebook[],"[[SelectionPlaceholder]]"]}], MenuKey["]", Modifiers->{"Command", "Option"}]], MenuItem["Matching {}", FrontEndExecute[{ FrontEnd`NotebookApply[ FrontEnd`InputNotebook[],"{[SelectionPlaceholder]}"]}], MenuKey["}", Modifiers->{"Command", "Option"}]], MenuItem["Matching ()", FrontEndExecute[{ FrontEnd`NotebookApply[ FrontEnd`InputNotebook[],"([SelectionPlaceholder])"]}], MenuKey[")", Modifiers->{"Command", "Option"}]], MenuItem["Matching [LeftDoubleBracket][RightDoubleBracket]", FrontEndExecute[{ FrontEnd`NotebookApply[ FrontEnd`InputNotebook[],"[LeftDoubleBracket][SelectionPlaceholder][RightDoubleBracket]"]}], MenuKey["]", Modifiers->{"Command", "Option", "Control"}]], |

These changes should survive minor updates to Mathematica, but it’s best to keep a copy of your file in case some later update replaces it.

The post Adding a keyboard shortcut to Mathematica V7 appeared first on Stoney's Zone.

]]>The post Pixel-perfect Graphics, Revisited appeared first on Stoney's Zone.

]]>

Instead of using ArrayPlot to generate the image, I can now do this:

myImage = Image[imageData, ColorSpace->RGBColor]

I still need to specify RGBColor to interpret the alpha channel correctly, but the option to Image is named ColorSpace, instead of the ColorFunction used by ArrayPlot.

Exporting works the same way as before:

Export["~/Desktop/myImage.png", myImage]

Except that now I can directly export a png with alpha. Much simpler overall in the new Mathematica.

The post Pixel-perfect Graphics, Revisited appeared first on Stoney's Zone.

]]>The post What is a sampling test image? appeared first on Stoney's Zone.

]]>The answer to the second question is simpler: pixel manipulation code often zeros the RGB channels of a pixel when it sees that the alpha is zero, which could interfere with the test.

The answer to the first question is more complicated as it requires some background. Combining pixels by sampling, blurring, or compositing them generally requires the pixels to be *premultiplied* first. If this isn’t done, then the result can show incorrect colors, halos, and other artifacts.

For example, this test image has alternating pixels of {1.0, 0.0, 0.0, 0.01} and {0.0, 0.0, 0.0, 1.0}, read as {Red, Green, Blue, Alpha}:

When blurred in Adobe After Effects CS3, using the included directional blur filter set to 3 degrees, we get this:

You can easily see that the blur did not premultiply the pixels before sampling them. The red channel has leaked out from the transparent pixels and polluted the image.

We can use a simple *average* operation to show why this happens. Note that we are starting with *straight* pixels, where the alpha channel is treated as a mask independent of the RGB channels.

The quick-but-wrong way to average two straight pixels is to simply add their alpha, red, green, and blue channels independently and divide the result values by two. When averaging two adjacent pixels in our test image, we would add a red value of 1 to a red value of 0, resulting in 0.5 after the division. Similarly, the alpha values average to 0.5005. The result is a reddish, half-transparent pixel. Since a blur is just a complicated average, that’s what happened in our test.

The right way to average straight pixels is to first multiply each of the color channels by the alpha value, then do the average. Afterwards, you have to divide the result color channels by the result alpha to produce straight pixels again. In our example, we should average a red value of 1 x 0.01 with one of 0 x 1. The result is a red value of 0.005 and an alpha of 0.5005 (as before). This is a near-black, half-transparent color, and is what we expect visually. The final, straightened red value is 0.005 / 0.5005, or 0.00999.

In general, premultiplying pixels before combining them, and straightening them afterwards, accounts for most of the time spent in these operations. Time that is actually wasted on fully-opaque pixels, where premultiplication has no effect.

This is a surprisingly common problem in practice. I’ve seen a commercial compositing application that failed to premultiply with any pixel-combining operations. The reaction that I got when I explained the need for premultiplication to the developers showed that they really didn’t understand the problem, and that they didn’t see it in practice. I suspect that this would show up mostly on feathered edges of layered images, and would either be mistaken for noise in the original image, or won’t be recognized as a bug since they weren’t looking for it.

It takes extra code and complexity to combine straight pixels properly, and to avoid unnecessary premultiplication and straightening, but it doesn’t matter how fast an implementation is if it produces the wrong result.

For more information on this topic, see the Wikipedia article on Alpha Compositing.

The post What is a sampling test image? appeared first on Stoney's Zone.

]]>The post Exporting Pixel-Perfect Graphics in Mathematica appeared first on Stoney's Zone.

]]>Here’s a simple image function that generates a grid of black pixels alternating with nearly-transparent red pixels. I use this sort of image to ensure that I’m sampling correctly.

imageFunction[x_, y_] := If[BitAnd[x, 1] == BitAnd[y, 1], {0, 0, 0, 1}, {1, 0, 0, 0.01}]

Table then makes a 256 x 128 array of RGBA values from the image function. This is non-square for this example to ensure that the width and height are in the right order later.

imageData = Table[ imageFunction[x, y], {y, 0, 127}, {x, 0, 255}];

ArrayPlot makes the pixel array viewable by converting it into a Graphics. The ColorFunction produces RGBA pixels (the default ignores alpha), PixelConstrained aligns the image to exact pixel boundaries, and ImageSize produces a result that’s exactly the right size.

apic = ArrayPlot[ imageData, ColorFunction -> RGBColor, PixelConstrained -> True, ImageSize -> Reverse[Take[Dimensions[imageData], 2]]]

This produces the image:

Which is then exported like this:

Export["~/Desktop/APic.tif", apic];

The result is a pixel-perfect file resulting from a raster image defined in Mathematica.

The post Exporting Pixel-Perfect Graphics in Mathematica appeared first on Stoney's Zone.

]]>