I can now reconstruct and iterate over elements from this radix tree of chemical symbols:

Code:
"ABCDEFGHIKLMNOPRSTUVWXYZcglmrstu-aehikr-adeflmnorsubsyrsu-elmrade-efgos-nr-rairuvdgnot-abdeiop-s-abdmortuabefghnu-bcegimnrabcehilm-u--e-bnr------------------------------------------------------------------------------------------------opst--------→Str1
{24+2[i],8+26[i],7+34[i],12+41[i],3+53[i],3+56[i],5+59[i],3+64[i],6+67[i],3+73[i],2+76[i],5+78[i],5+83[i],8+88[i],2+96[i],9+98[i],8+107[i],9+115[i],8+124[i],2+132[i],1+134[i],1+135[i],1+136[i],2+137[i],2+139[i],1+141[i],1+142[i],1+143[i],1+144[i],1+145[i],1+146[i],1+147[i],1+148[i],0,1+149[i],1+150[i],1+151[i],1+152[i],1+153[i],1+154[i],0,1+155[i],1+156[i],1+157[i],1+158[i],1+159[i],1+160[i],1+161[i],1+162[i],1+163[i],1+164[i],1+165[i],1+166[i],1+167[i],1+168[i],1+169[i],1+170[i],1+171[i],0,1+172[i],1+173[i],1+174[i],1+175[i],1+176[i],1+177[i],1+178[i],0,1+179[i],1+180[i],1+181[i],1+182[i],1+183[i],0,1+184[i],1+185[i],0,1+186[i],1+187[i],1+188[i],1+189[i],1+190[i],1+191[i],1+192[i],1+193[i],1+194[i],1+195[i],1+196[i],0,1+197[i],1+198[i],1+199[i],1+200[i],1+201[i],1+202[i],1+203[i],0,1+204[i],0,1+205[i],1+206[i],1+207[i],1+208[i],1+209[i],1+210[i],1+211[i],1+212[i],1+213[i],1+214[i],1+215[i],1+216[i],1+217[i],1+218[i],1+219[i],1+220[i],0,1+221[i],1+222[i],1+223[i],1+224[i],1+225[i],1+226[i],1+227[i],1+228[i],1+229[i],1+230[i],1+231[i],1+232[i],1+233[i],1+234[i],1+235[i],1+236[i],0,4+237[i],0,0,1+241[i],0,1+242[i],1+243[i],1+244[i],0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1+245[i],1+246[i],1+247[i],1+248[i],0,0,0,0,0,0,0,0→L1


The goal is to have a predictive-input interface.
For those that are like myself (probably not many) could you dumb this down a bit? What does this do? could we see an animated screenshot? Smile
Right now the technically interesting (to programmers part) is just reconstructing the element names from a radix tree in a language that doesn't have pointers or recursion. "Ac", "Ag", and "Al" are Actinium, Silver, and Aluminum, respectively. You'll notice that none of those strings actually appear in the text I posted. Instead the list entry corresponding to "A" is 8+26[i] (everything is offset by one in the list, because the root of the tree doesn't generate text), which says "there are 8 children, beginning at index 26". The first three of those children are "c", "g", and "l", each of which have a single leaf node representing "generate a string from this path from the root to the leaf". In the string there is a placeholder "-" to maintain spacing.

The goal here is predictive text input (ala "T9" mode on old cellphones), to make sure you input correct chemical symbols.

There's no animated screenshot yet because everything isn't working yet, just the initial parsing.

The goal is to have a program that takes chemical formulas and balances them. e.g. "Zn + HCl → ZnCl2 + H2" balances as "Zn + 2HCl → ZnCl2 + H2". That part is basically trivial linear algebra though (n unknowns, n-1 equations, and choose one unknown = 1 to make everything else be integers). The interesting part for me is building the interface.

[edit]
Here's a screenshot. No UI glitz, but the framework is there. Should run a little faster once I get rid of some extraneous tokens that are likely slowing down parsing (I have comment strings and :::: for indentation).


Code:
:augment({24,8,7,12,3,3,5,3,6,3,2,5,5,8,2,9,8,9,8,2,1,1,1,2,2},augment(seq(min(X≠{9,16,34,42,48,51,63,71,73,90}),X,1,106),augment({0,4,0,0,1,0,1,1,1},2>abs(98.5-cumSum(binomcdf(106,0
:augment(Ans,{0})+iaugment(Ans xor 0,{0})cumSum(augment({2},Ans→L1

This is the same list, prepared in 966 fewer bytes of code. I'm sorry to say that it's pretty taxing on memory (~9K bytes), being a complex list type and all. Wink
Like a champ! Also, is this particular binomcdf evil in the way that you were describing on IRC with memory corruption?
I think it's specific to the generally bug-riddled 2.21. That's an old topic that I think BrandonW knew something about.
Ah, ok, I should be set then, I think I'm on the latest non-MP.
So I'm playing around with this again after a while and been slightly frustrated with the speed of my tree traversals.

I suspect this:

Code:
      // We assume as an invariant:
      // dim(L5)-dim(L3)=dim(L6)-dim(L4) > 0
      While (dim(L6)-dim(L4)) and (L6(dim(L6))=real(L5(dim(L5)))-1+imag(L5(dim(L5
         dim(L6)-1->dim(L6
         dim(L5)-1->dim(L5
      End

And this:

Code:
      L1(L4(dim(L4
      For(P,imag(Ans),real(Ans)-1+imag(Ans
         If sub(Str5,length(Str5),1)=sub(Str1,P-1,1:Then
            Ans->L3(1+dim(L3
            P->L4(1+dim(L4
         End
      End


Could both be optimized somewhat using seq() and friends. I have a decent idea of how to do the second one, but am not convinced the SortA() SortD() overhead will be worthwhile, but the first is a "find the first element s.t." which is trickier (and made slightly worse by being an rtl scan, rather than an ltr scan).

Any thoughts? (Looking at you goose)

[edit]

Also, bonus problem:

Code:
      While abs(L1(L6(dim(L6
         L1(L6(dim(L6->L5(1+dim(L5
         imag(L5(dim(L5->L6(1+dim(L6
      End


The data dependencies here seem way too tightly interlaced to make good use of seq(), but I'm open to suggestions.

Code:
L6(dim(L6
While abs(L1(Ans
   L1(Ans->L5(1+dim(L5
   imag(Ans->L6(1+dim(L6
End
Can you give some examples of what these lists/strings may hold? Specifically, examples that represent the kind of cases you're most concerned about speeding up?
L1 is a huge list (see first post) encoding a radix tree. L3/L4 and L5/L6 are paired stacks representing paths from the root of the tree. Entries in L3 or L5 represent node data that has been copied from L1, and the entry at the same index in L4 or L6 is the index from which it was copied. The node data itself has two components (hence real/imag): An index to the first child, and the number of children (which are stored consecutively in the list, so you can easily iterate over a sublist to visit all children).

The stacks will always be relatively shallow as chemical symbols top out at 3 letters.

[edit]

I should mention however that the first and third snippets I posted (the two While loops) are both inner loops that will run up to 5 times each before anything can be shown to the user, which is why they have to be pretty tight. The second snippet runs once after any letter key is pressed to potentially select a specific child node. The number of iterations there is dependent on the number of children to be inspected.

[edit 2]
Replaced the substring loop with this:

Code:
      L1(L4(dim(L4->Q
      sub(Str5,length(Str5),1
      max(seq(P(Ans=sub(Str1,P-1,1)),P,imag(Q),real(Q)-1+imag(Q
      If Ans:Then
         Ans->L4(1+dim(L4
         Q->L3(1+dim(L3
      End


The first snippet I replaced with:

Code:
      max(seq(P(L6(P)!=real(L5(P))-1+imag(L5(P))),P,1+dim(L4),dim(L6)
      max(Ans,dim(L4
      Ans->dim(L5
      Ans->dim(L6


and the third with:

Code:
      L6(dim(L6
      While abs(L1(Ans
         L1(Ans->L5(1+dim(L5
         imag(Ans->L6(1+dim(L6
      End
As previously suggested in this thread.

Also updated the UI loop to be a little more responsive, but it still feels a bit slow to offer completions, so any further suggestions would be appreciated.


I'd also like to add the ability to scroll sideways to see additional completions, but otherwise this input routine is full-featured and, barring optimizations, it's time to integrate it into an equation editor.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement