| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809 |
- /*
- * This file is a part of TTMath Mathematical Library
- * and is distributed under the (new) BSD licence.
- * Author: Tomasz Sowa <[email protected]>
- */
- /*
- * Copyright (c) 2006-2010, Tomasz Sowa
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * * Neither the name Tomasz Sowa nor the names of contributors to this
- * project may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
- #ifndef headerfilettmathobject
- #define headerfilettmathobject
- /*!
- \file ttmathobjects.h
- \brief Mathematic functions.
- */
- #include <string>
- #include <vector>
- #include <list>
- #include <map>
- #include "ttmathtypes.h"
- #include "ttmathmisc.h"
- namespace ttmath
- {
- /*!
- objects of this class are used with the mathematical parser
- they hold variables or functions defined by a user
- each object has its own table in which we're keeping variables or functions
- */
- class Objects
- {
- public:
- /*!
- one item (variable or function)
- 'items' will be on the table
- */
- struct Item
- {
- // name of a variable of a function
- // internally we store variables and funcions as std::string (not std::wstring even when wide characters are used)
- std::string value;
- // number of parameters required by the function
- // (if there's a variable this 'param' is ignored)
- int param;
- Item() {}
- Item(const std::string & v, int p) : value(v), param(p) {}
- };
- // 'Table' is the type of our table
- typedef std::map<std::string, Item> Table;
- typedef Table::iterator Iterator;
- typedef Table::const_iterator CIterator;
- /*!
- this method returns true if a character 'c' is a character
- which can be in a name
-
- if 'can_be_digit' is true that means when the 'c' is a digit this
- method returns true otherwise it returns false
- */
- static bool CorrectCharacter(int c, bool can_be_digit)
- {
- if( (c>='a' && c<='z') || (c>='A' && c<='Z') )
- return true;
- if( can_be_digit && ((c>='0' && c<='9') || c=='_') )
- return true;
- return false;
- }
- /*!
- this method returns true if the name can be as a name of an object
- */
- template<class string_type>
- static bool IsNameCorrect(const string_type & name)
- {
- if( name.empty() )
- return false;
- if( !CorrectCharacter(name[0], false) )
- return false;
- typename string_type::const_iterator i = name.begin();
- for(++i ; i!=name.end() ; ++i)
- if( !CorrectCharacter(*i, true) )
- return false;
-
- return true;
- }
- /*!
- this method returns true if such an object is defined (name exists)
- */
- bool IsDefined(const std::string & name)
- {
- Iterator i = table.find(name);
- if( i != table.end() )
- // we have this object in our table
- return true;
- return false;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method returns true if such an object is defined (name exists)
- */
- bool IsDefined(const std::wstring & name)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return false;
- Misc::AssignString(str_tmp1, name);
- return IsDefined(str_tmp1);
- }
- #endif
- /*!
- this method adds one object (variable of function) into the table
- */
- ErrorCode Add(const std::string & name, const std::string & value, int param = 0)
- {
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Iterator i = table.find(name);
- if( i != table.end() )
- // we have this object in our table
- return err_object_exists;
- table.insert( std::make_pair(name, Item(value, param)) );
- return err_ok;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method adds one object (variable of function) into the table
- */
- ErrorCode Add(const std::wstring & name, const std::wstring & value, int param = 0)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, name);
- Misc::AssignString(str_tmp2, value);
-
- return Add(str_tmp1, str_tmp2, param);
- }
- #endif
- /*!
- this method returns 'true' if the table is empty
- */
- bool Empty() const
- {
- return table.empty();
- }
- /*!
- this method clears the table
- */
- void Clear()
- {
- return table.clear();
- }
- /*!
- this method returns 'const_iterator' on the first item on the table
- */
- CIterator Begin() const
- {
- return table.begin();
- }
- /*!
- this method returns 'const_iterator' pointing at the space after last item
- (returns table.end())
- */
- CIterator End() const
- {
- return table.end();
- }
- /*!
- this method changes the value and the number of parameters for a specific object
- */
- ErrorCode EditValue(const std::string & name, const std::string & value, int param = 0)
- {
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Iterator i = table.find(name);
- if( i == table.end() )
- return err_unknown_object;
-
- i->second.value = value;
- i->second.param = param;
- return err_ok;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method changes the value and the number of parameters for a specific object
- */
- ErrorCode EditValue(const std::wstring & name, const std::wstring & value, int param = 0)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, name);
- Misc::AssignString(str_tmp2, value);
-
- return EditValue(str_tmp1, str_tmp2, param);
- }
- #endif
- /*!
- this method changes the name of a specific object
- */
- ErrorCode EditName(const std::string & old_name, const std::string & new_name)
- {
- if( !IsNameCorrect(old_name) || !IsNameCorrect(new_name) )
- return err_incorrect_name;
- Iterator old_i = table.find(old_name);
- if( old_i == table.end() )
- return err_unknown_object;
-
- if( old_name == new_name )
- // the new name is the same as the old one
- // we treat it as a normal situation
- return err_ok;
- ErrorCode err = Add(new_name, old_i->second.value, old_i->second.param);
-
- if( err == err_ok )
- {
- old_i = table.find(old_name);
- TTMATH_ASSERT( old_i != table.end() )
- table.erase(old_i);
- }
- return err;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method changes the name of a specific object
- */
- ErrorCode EditName(const std::wstring & old_name, const std::wstring & new_name)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(old_name) || !IsNameCorrect(new_name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, old_name);
- Misc::AssignString(str_tmp2, new_name);
- return EditName(str_tmp1, str_tmp2);
- }
- #endif
- /*!
- this method deletes an object
- */
- ErrorCode Delete(const std::string & name)
- {
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Iterator i = table.find(name);
- if( i == table.end() )
- return err_unknown_object;
- table.erase( i );
- return err_ok;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method deletes an object
- */
- ErrorCode Delete(const std::wstring & name)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, name);
- return Delete(str_tmp1);
- }
-
- #endif
- /*!
- this method gets the value of a specific object
- */
- ErrorCode GetValue(const std::string & name, std::string & value) const
- {
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- CIterator i = table.find(name);
- if( i == table.end() )
- {
- value.clear();
- return err_unknown_object;
- }
- value = i->second.value;
- return err_ok;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method gets the value of a specific object
- */
- ErrorCode GetValue(const std::wstring & name, std::wstring & value)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, name);
- ErrorCode err = GetValue(str_tmp1, str_tmp2);
- Misc::AssignString(value, str_tmp2);
- return err;
- }
- #endif
- /*!
- this method gets the value of a specific object
- (this version is used for not copying the whole string)
- */
- ErrorCode GetValue(const std::string & name, const char ** value) const
- {
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- CIterator i = table.find(name);
- if( i == table.end() )
- {
- *value = 0;
- return err_unknown_object;
- }
- *value = i->second.value.c_str();
- return err_ok;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method gets the value of a specific object
- (this version is used for not copying the whole string)
- */
- ErrorCode GetValue(const std::wstring & name, const char ** value)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, name);
- return GetValue(str_tmp1, value);
- }
- #endif
- /*!
- this method gets the value and the number of parameters
- of a specific object
- */
- ErrorCode GetValueAndParam(const std::string & name, std::string & value, int * param) const
- {
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- CIterator i = table.find(name);
- if( i == table.end() )
- {
- value.empty();
- *param = 0;
- return err_unknown_object;
- }
- value = i->second.value;
- *param = i->second.param;
- return err_ok;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method gets the value and the number of parameters
- of a specific object
- */
- ErrorCode GetValueAndParam(const std::wstring & name, std::wstring & value, int * param)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, name);
- ErrorCode err = GetValueAndParam(str_tmp1, str_tmp2, param);
- Misc::AssignString(value, str_tmp2);
- return err;
- }
- #endif
- /*!
- this method sets the value and the number of parameters
- of a specific object
- (this version is used for not copying the whole string)
- */
- ErrorCode GetValueAndParam(const std::string & name, const char ** value, int * param) const
- {
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- CIterator i = table.find(name);
- if( i == table.end() )
- {
- *value = 0;
- *param = 0;
- return err_unknown_object;
- }
- *value = i->second.value.c_str();
- *param = i->second.param;
- return err_ok;
- }
- #ifndef TTMATH_DONT_USE_WCHAR
- /*!
- this method sets the value and the number of parameters
- of a specific object
- (this version is used for not copying the whole string
- but in fact we make one copying during AssignString())
- */
- ErrorCode GetValueAndParam(const std::wstring & name, const char ** value, int * param)
- {
- // we should check whether the name (in wide characters) are correct
- // before calling AssignString() function
- if( !IsNameCorrect(name) )
- return err_incorrect_name;
- Misc::AssignString(str_tmp1, name);
- return GetValueAndParam(str_tmp1, value, param);
- }
- #endif
- /*!
- this method returns a pointer into the table
- */
- Table * GetTable()
- {
- return &table;
- }
- private:
- Table table;
- std::string str_tmp1, str_tmp2;
- }; // end of class Objects
- /*!
- objects of the class History are used to keep values in functions
- which take a lot of time during calculating, for instance in the
- function Factorial(x)
- it means that when we're calculating e.g. Factorial(1000) and the
- Factorial finds that we have calculated it before, the value (result)
- is taken from the history
- */
- template<class ValueType>
- class History
- {
- /*!
- one item in the History's object holds a key, a value for the key
- and a corresponding error code
- */
- struct Item
- {
- ValueType key, value;
- ErrorCode err;
- };
- /*!
- we use std::list for simply deleting the first item
- but because we're searching through the whole container
- (in the method Get) the container should not be too big
- (linear time of searching)
- */
- typedef std::list<Item> buffer_type;
- buffer_type buffer;
- typename buffer_type::size_type buffer_max_size;
- public:
-
- /*!
- default constructor
- default max size of the History's container is 15 items
- */
- History()
- {
- buffer_max_size = 15;
- }
- /*!
- a constructor which takes another value of the max size
- of the History's container
- */
- History(typename buffer_type::size_type new_size)
- {
- buffer_max_size = new_size;
- }
- /*!
- this method adds one item into the History
- if the size of the container is greater than buffer_max_size
- the first item will be removed
- */
- void Add(const ValueType & key, const ValueType & value, ErrorCode err)
- {
- Item item;
- item.key = key;
- item.value = value;
- item.err = err;
- buffer.insert( buffer.end(), item );
- if( buffer.size() > buffer_max_size )
- buffer.erase(buffer.begin());
- }
- /*!
- this method checks whether we have an item which has the key equal 'key'
- if there's such item the method sets the 'value' and the 'err'
- and returns true otherwise it returns false and 'value' and 'err'
- remain unchanged
- */
- bool Get(const ValueType & key, ValueType & value, ErrorCode & err)
- {
- typename buffer_type::iterator i = buffer.begin();
- for( ; i != buffer.end() ; ++i )
- {
- if( i->key == key )
- {
- value = i->value;
- err = i->err;
- return true;
- }
- }
- return false;
- }
- /*!
- this methods deletes an item
- we assume that there is only one item with the 'key'
- (this methods removes the first one)
- */
- bool Remove(const ValueType & key)
- {
- typename buffer_type::iterator i = buffer.begin();
- for( ; i != buffer.end() ; ++i )
- {
- if( i->key == key )
- {
- buffer.erase(i);
- return true;
- }
- }
- return false;
- }
- }; // end of class History
- /*!
- this is an auxiliary class used when calculating Gamma() or Factorial()
- in multithreaded environment you can provide an object of this class to
- the Gamma() or Factorial() function, e.g;
- typedef Big<1, 3> MyBig;
- MyBig x = 123456;
- CGamma<MyBig> cgamma;
- std::cout << Gamma(x, cgamma);
- each thread should have its own CGamma<> object
- in a single-thread environment a CGamma<> object is a static variable
- in a second version of Gamma() and you don't have to explicitly use it, e.g.
- typedef Big<1, 3> MyBig;
- MyBig x = 123456;
- std::cout << Gamma(x);
- */
- template<class ValueType>
- struct CGamma
- {
- /*!
- this table holds factorials
- 1
- 1
- 2
- 6
- 24
- 120
- 720
- .......
- */
- std::vector<ValueType> fact;
- /*!
- this table holds Bernoulli numbers
- 1
- -0.5
- 0.166666666666666666666666667
- 0
- -0.0333333333333333333333333333
- 0
- 0.0238095238095238095238095238
- 0
- -0.0333333333333333333333333333
- 0
- 0.075757575757575757575757576
- .....
- */
- std::vector<ValueType> bern;
- /*!
- here we store some calculated values
- (this is for speeding up, if the next argument of Gamma() or Factorial()
- is in the 'history' then the result we are not calculating but simply
- return from the 'history' object)
- */
- History<ValueType> history;
- /*!
- this method prepares some coefficients: factorials and Bernoulli numbers
- stored in 'fact' and 'bern' objects
-
- how many values should be depends on the size of the mantissa - if
- the mantissa is larger then we must calculate more values
- for a mantissa which consists of 256 bits (8 words on a 32bit platform)
- we have to calculate about 30 values (the size of fact and bern will be 30),
- and for a 2048 bits mantissa we have to calculate 306 coefficients
- you don't have to call this method, these coefficients will be automatically calculated
- when they are needed
- you must note that calculating these coefficients is a little time-consuming operation,
- (especially when the mantissa is large) and first call to Gamma() or Factorial()
- can take more time than next calls, and in the end this is the point when InitAll()
- comes in handy: you can call this method somewhere at the beginning of your program
- */
- void InitAll();
- // definition is in ttmath.h
- };
- } // namespace
- #endif
|