[go: up one dir, main page]

Menu

[r8]: / src / unit.h  Maximize  Restore  History

Download this file

222 lines (184 with data), 8.6 kB

  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
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************************
unit.h - Header file for class unit, an extension class to GiNaC
-------------------
begin : Mon Oct 22 2001
copyright : (C) 2001 by Jan Rheinlaender
email : jrheinlaender@users.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef UNIT_H
#define UNIT_H
/**
* @author Jan Rheinlaender
* @short Extends GiNaC to handle physical units, i.g., "x = 1 mm", "1 m + 1 mm = 1,001 m"
*/
#include "utils.h"
#include <string>
#include <stdexcept>
#include <ginac/ginac.h>
using namespace GiNaC;
#include "expression.h" // *** added in 0.4
/// Helper class to initialize class Unit *** added in 0.5
class unit_init {
public:
unit_init();
~unit_init();
private:
/// A boolean used to ensure that init() is only called once in the lifetime of the class
static bool called;
static unit_init unit_initializer;
};
/**
* Internal GiNaC identifier for class Unit
*/
//const unsigned TINFO_Unit = 0x42420001U; *** removed in 1.2.1
/// Extends GiNaC to handle physical units
class Unit : public basic {
GINAC_DECLARE_REGISTERED_CLASS(Unit, basic)
public:
friend class unit_init;
/**
* Construct a Unit with the given name.
* @param n A string with the name of the unit
*/
Unit(const std::string &n);
/**
* Print the unit in a GiNaC print context.
* @param c The print context (e.g., print_latex)
* @param level Unused, for consistency with GiNaC print methods
*/
// *** changed name to do_print in 0.8
void do_print(const print_context &c, unsigned level = 0) const;
/**
* Return the name of the unit
* @returns A string with the name of the unit
*/
inline const std::string get_name() const { return(name); }
/// Set the print name of the unit
inline void set_printname(const std::string& pn) { printname = pn; }
/// Set the expression to be printed for this unit
inline void set_printex(const expression& e) { printex = e; } // *** added in 1.4.1
/// Get the print name of the unit
inline const std::string get_printname() const { return printname; } // *** changed in 1.4.1
/// Get the expression to be printed for this unit
inline const expression get_printex() const { return printex; } // *** added in 1.4.1
/**
* Required by GiNac, does nothing. It would be possible to define simplification or
* canonicalization rules here
* @param level Unused, required by GiNaC
* @returns An expression with the result of the evaluation
*/
ex eval(int level = 0) const;
/// archive the unit
void archive(archive_node& n) const; // *** added in 1.3.1
/// unarchive the unit
void read_archive(const archive_node& n, lst& syms); // *** added in 1.3.1
// static (class) functions
/// Return the Unit for the given Unit name. This Unit is unique and the same Unit is returned every time
static Unit get(const std::string &n); // *** added in 1.4.1
static Unit* getp(const std::string &n);
/**
* Looks the unit given by its name up in the conversion table, and returns an equivalent expression of
* base units.
* Issues an error if there is no entry for this unit name in the conversion table, and returns a (base)
* unit with the name "unknown".
* @param unit A string with the name of the unit that is to be looked up
* @returns An expression with the units corresponding to the name given
*/
static expression canonicalize_from(const std::string &unit); // *** changed ex to expression in 0.4
/**
* Takes an expression and turns all units in it to base units. The expression must be a unit or a mul of
* units and their powers. Any numerics found are left untouched. Symbols are accepted as well, to
* allow for constants like \\pi. If any other GiNaC types are found in the expression, the method issues
* a warning and leaves them untouched.
* @param units The expression that is to be canonicalized
* @returns An expression containing only base units, numerics and symbols
*/
static expression canonicalize(const expression &units); // *** changed ex to expression in 0.4
/**
* Adds a unit name to the list of unit names.
* @param uname The name of the unit
*/
static void add_unitname(const std::string &uname);
/**
* Add a new unit conversion rule to the class. The unit with the name "uname" can be expressed
* by the expression "other_units". This method issues a warning if a conversion with this name
* already exists, and overwrites it with the new one.
* @param uname The unit name for which the conversion is to be added
* @param other_units An expression with the units which are equivalent to "uname"
*/
static void add_conversion(const std::string &uname, const expression &other_units);
/** // *** added in 0.3, changed back to use lst in 0.9, changed back to vector in 1.0, made public in 1.2
* Create the conversion list from a list of preferred units
* @param units A vector of the preferred units
* @returns A vector of the necessary conversions
*/
// Note: The method returns a pointer for efficiency reasons
static std::vector<relational> *create_conversions(const std::vector<expression> &units);
/// Set option to a list value
// *** replaces set_preferred_units in 1.1
// *** moved to eqc.cpp/optstack.cpp in 1.2
/**
* Returns true if the unit name was previously registered using add_unitname, or is a base unit
* @param uname The unit name
* @returns True if the unit name is find in the list of unit names
*/
static bool is_unitname(const std::string &uname);
static bool is_unitnamep(const std::string &uname);
/** // *** renamed and added parameter l in 0.3, added parameter unitlist in 1.4.1
* Substitutes all units in the expression with the users preferred units.
* If this function does not give the
* desired result, check whether the preferred units should be registered in a different order!
* @param e The expression in which the units are to be substituted
* @returns An expression with the result of the substitution
*/
static expression subst_units(const expression &e, const std::vector<relational>& unitlist);
/**
* Return the unit conversion table and the list of known unit names to the state after init()
*/
static void clear();
/// Print class information onto a stream
static void print(std::ostream &os);
/// Print class information onto a message stream
//static void print(message &ms); // *** added in 0.7, changed in 1.3.1
private:
/// The name of the unit
std::string name;
/// The name to be used for printing
std::string printname; // *** added in 1.3.1 for iMath
/// The expression to be used for printing // *** added in 1.4.1 for iMath
expression printex;
// *** added in 1.4.1
/// A map of the unitnames -> Units to consistently return the same object for a unitname
static std::map<std::string, Unit*> factory;
/// A map with the unit conversions
static std::map<std::string, expression> conversions; // *** changed ex to expression in 0.4
// /// A set containing the names of the base units
// static std::set<const char*, ltstr> base_units; *** removed in 1.4.1.
/// A set of unit names that could be encountered by latex parsing
static std::set<const char*, ltstr> unitnames;
static bool initialized; // *** added in 1.3.1
public:
/**
* Initialize the unit conversion table and the list of known unit names with the SI base units
*/
static void init(); // *** made public in 1.3.1
// *** moved here in 1.3.1
static Unit* metre;
static Unit* kilogram;
static Unit* second;
static Unit* ampere;
static Unit* kelvin;
static Unit* celsius;
static Unit* mole;
static Unit* candela;
};
GINAC_DECLARE_UNARCHIVER(Unit);
#endif