Ternary VM (TVM)

Balanced Ternary Numeral System - forum was moved from http://ternary.info

Moderator: haqreu

hemuman

Re: Ternary VM (TVM)

Post by hemuman »

It should look better now.

Code: Select all

String add(String[] a, String[] b)
{
String result_s="";
int lcv=0;
int[] result = {0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; // J Note that the array is N+1 trits long, and the last one is carry M.
if(a.length>b.length)
{
lcv=a.length;
}else{
lcv=b.length;
}
for(int i=0;i<lcv;i++)
{
int a1=Integer.valueOf(a[i]);
int b1=Integer.valueOf(b[i]);
if(((a1==1)&&(b1==1))||((a1==-1)&&(b1==-1))){
String[] tryte_=(add_rule(a[i],b[i])).split(",");
int temp=(Integer.parseInt(tryte_[0]));
if(((temp==1)&&(result[i]==1))||((temp==-1)&&(result[i]==-1))){
String[] tryte_1=(add_rule(Integer.toString(result[i]),Integer.toString(temp))).split(",");
result[i]=(Integer.parseInt(tryte_1[0]));
result[i+1]=(Integer.parseInt(tryte_1[1]));
}else{
result[i]=Integer.parseInt((add_rule(Integer.toString(result[i]),Integer.toString(temp))));
}

temp=(Integer.parseInt(tryte_[1]));
if(((temp==1)&&(result[i+1]==1))||((temp==-1)&&(result[i+1]==-1))){
String[] tryte_1=(add_rule(Integer.toString(result[i+1]),Integer.toString(temp))).split(",");
result[i+1]=(Integer.parseInt(tryte_1[0]));
//result[i+1]=(Integer.parseInt(tryte_1[1]));
}else{
result[i+1]=Integer.parseInt((add_rule(Integer.toString(result[i+1]),Integer.toString(temp))));
}

}
else
{
int temp=(Integer.parseInt((add_rule(a[i],b[i]))));
if(((temp==1)&&(result[i]==1))||((temp==-1)&&(result[i]==-1))){
String[] tryte_1=(add_rule(Integer.toString(result[i]),Integer.toString(temp))).split(",");
result[i]=(Integer.parseInt(tryte_1[0]));
result[i+1]=(Integer.parseInt(tryte_1[1]));
}else{
result[i]=Integer.parseInt((add_rule(Integer.toString(result[i]),Integer.toString(temp))));
}
}
result_s=result_s+Integer.toString(result[i]);
}

result_s=result_s+Integer.toString(result[lcv]);
return result_s;
}
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

Hello all,

below is the Java Class file which can be used for ternary
addition and multiplication.


Ternary API.
TernaryMath.class
This contains following methods:
Compiled from "TernaryMath.java"
class TernaryMath extends java.lang.Object{
TernaryMath();
java.lang.String add_rule(java.lang.String, java.lang.String);
java.lang.String add(java.lang.String[], java.lang.String[]);
java.lang.String subtract(java.lang.String[], java.lang.String[]);
java.lang.String multyply(java.lang.String[], java.lang.String[]);
}
http://TernaryMath.class


Usage:
The input:
Please provide input only as array of trits.


Also,
Please post your views on same.

Thanks,
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary VM (TVM)

Post by Mac Buster »

It would be just great if you can also provide link to source code of the class.

Please correct method name from multyply to multiply.
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

Below is the location of the source file:

http://tvm.manojky.net/tmath.php
or
http://tvm.manojky.net/TernaryMath.java
TernaryMath.java

Corrected multyply to multiply.
class TernaryMath extends java.lang.Object{
TernaryMath();
java.lang.String add_rule(java.lang.String, java.lang.String);
java.lang.String add(java.lang.String[], java.lang.String[]);
java.lang.String subtract(java.lang.String[], java.lang.String[]);
java.lang.String multiply(java.lang.String[], java.lang.String[]);
}
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

Hello All,

Now that i can perform addition and multiplication, can any one suggest me how should i store the information in the file in ternary format, Basically I am looking for file format for ternary.

I have already selected the Tryte codes for Ascii Chars, they are available at following location,

http://manojky.net/terPrj/TernaryWithCharC.htm
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary VM (TVM)

Post by eudoxie »

In tunguska, I store the integer values in an array type signed short, which is subsequently compressed with zlib when saved to a file.

This has the advantage of being very fast and very space effective (since a memory image will have lots of zeros).

I suppose you can also save the individual trits in pairs of bytes in a short. This is harder to manipulate, since you need to juggle around a lot of bits to extract and set values.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary VM (TVM)

Post by Mac Buster »

I have already selected the Tryte codes for Ascii Chars, they are available at following location,
It does not look very effective, since you do not use negative part of diapason that could be easily used for lower case letters and fractional numbers (1/2, 1/3, etc). Also I see no letters for Cyrillic (Russian, Ukrainian, and so on) and Greek alphabets. And some more mathematical symbols have to be added.
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

Hi,

I am planning for use following format,
Image
------------------------
I will store the instructions/definition/method into the METADATA file to read the DATA File, it will help the users to use their own file formats.


Mostly the METADATA file will be in XML Format
Please post your views.
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

Yes, I agree, I intentionally left it for future use, as nothing was striking to my mind,

If you can help me in preparing/utilizing the negative part, it will be great.
hemuman

Re: Ternary VM (TVM)

Post by hemuman »


Quote:

I have already selected the Tryte codes for Ascii Chars, they are available at following location,



It does not look very effective, since you do not use negative part of diapason that could be easily used for lower case letters and fractional numbers (1/2, 1/3, etc). Also I see no letters for Cyrillic (Russian, Ukrainian, and so on) and Greek alphabets. And some more mathematical symbols have to be added.




Yes, I agree, I intentionally left it for future use, as nothing was striking to my mind,

If you can help me in preparing/utilizing the negative part, it will be great.
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

I'll be going with the below mentioned table.
http://tvm.manojky.net/csv/TernaryWithCharC.htm
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

Hello All,

Finally I have prepared an applet which converts the ASCII text to tryte, and displays it based on the table mentioned above....

http://tvm.manojky.net/TryteEditor/


Please visit and post your views.
Alexandr
Novelist
Posts: 34
Joined: 20 Oct 2005 18:46

Re: Ternary VM (TVM)

Post by Alexandr »

Here http://trinary.ru/projects/bttencoder/ you can find a coder which do the same that your applet, but it use more effective algorithm (Saved space 33% vs 25%).
To see result press blue button.
You will see code in bytes In the first column and in trytes in the second column.
We use -0+ as equivalent of -1 0 1.

P.S.: The english version of the coder on plan. )) Don't use IE (it's not supported)
Good luck! :)
hemuman

Re: Ternary VM (TVM)

Post by hemuman »

Hi,

Its not possible to save space more or less than 25%,
its a constant value, b'coz,
1 char = 8 bits (1 Byte)
= 6 trits(1 Tryte),

so every time you add a char, you will save only
(1-6/8) ie. .25 = 25%, i am not sure if the application you mentioned works on ternary representation.
please varify.

btw it looks better than mine :-)

Thanks,
Alexandr
Novelist
Posts: 34
Joined: 20 Oct 2005 18:46

Re: Ternary VM (TVM)

Post by Alexandr »

Hi!

It is possible :)
2^8 = 256 variations, аnd 3^6 = 729 variations, when you code 8 bits into 6 trits 729-256 = 473 variations are not used.
It is too much.
In our coder we code every 3 bits into 2 trits.
In this case only one variation is not used (3^2 - 2^3 = 9-8 = 1).
That's why we have 33% economy (1-2/3).

What do you think about it?