Whenever I write a code with a solution using dynamic programming my code looks something like this:
table[1000][1000] //the cache to store initialized with a certain_value
function(parameters i,j){
if(base_condition){
return base_value
}
if(table[i][j] != certain_value){
return table[i][j];
}
answer = some operation using function();
table[i][j] = answer;
return answer;
}
Usually I choose this certain_value as -1. But now I am writing a code in which the function can return all real numbers. So how do I choose this value or should I change my approach.
You can use a parallel data structure of bool elements to represent which elements have been cached.
Alternatively, you can use std::optional as the element type, and let the empty value represent a non-cached value.
You could have a boolean array. If you once calculated for (i,j) then keep true on that boolean[i][j]. So when you are testing whether this state is precached or not just test using whether boolean[i][j] is true or not. If true then returned the stored value from table array.
boolean visited[1000][1000]={false}
table[1000][1000] //the cache to store intialized with a certain_value
function(parameters i,j){
if(base_condition){
return base_value
}
if(visited[i][j] == true{
return table[i][j];
}
answer = some operation using function();
table[i][j] = answer;
visited[i][j]=true;
return answer;
}
There are other approaches you could take. If you want to stick with the approach you are used to, this question is not about dynamic programming so much as it is about sentinel values. What value can you use as a sentinel if all real numbers are possible?
One detail I notice is that all real numbers are possible. That's both good and bad. It's bad because a computer cannot represent every real number, but presumably that has already been accounted for. It's good because most methods used to represent real numbers include some values that are not real numbers. The float or double values that most obviously are not real numbers are the NaNs (Not a Number). Another option is infinity. In either case, std::is_finite can be used to detect if the sentinel value has been changed to a real number.
Strictly speaking, it is not guaranteed that these values will be available, but in practice they likely are. When they are available, they can be used as sentinels as long as your special function cannot return them. (Double check the claim "all real numbers are possible" in terms of float/double values — can the function return a value that is not a real number?)
Related
int main ()
{
double largest; cin >> largest;
double input;
while (cin >> input)
{
if (input > largest)
{
largest = input;
}
}
}
Why are we taking largest as a user input when clearly that is what we want out of this code fragment?
If you don't set largest to something, you're going to have problems; having an unset stack value means largest has a garbage value and you'll get undefined behavior when you test if (input > largest). So that's the worst option; you don't want nasal demons.
You could initialize it -HUGE_VAL, or -DBL_MAX if you must avoid infinities (-std::numeric_limits<double>::infinity() and -std::numeric_limits<double>::max() or std::numeric_limits<double>::lowest() if you like to use C++ includes to simplify; the C headers distribute the limits for different types across multiple headers), instead to get a hugely negative value that any input would be larger than, and the code would be mostly the same, with the main difference being what happens if the user fails to provide any input; with a constant initializer, largest would remain that value (it never gets reassigned), while with cin initialization, it would get whatever cin does when it fails to parse a value (IIRC, for C++11 and later, 0 gets stored, before C++11 it stores nothing and we're still in nasal demons land).
The primary reason to initialize it from cin is just for simplicity; when we've only taken one input, then that input is the largest so far. As we loop, largest will remain the largest so far as we update it on demand, and when the loop is over, the largest so far also happens to be the largest overall, and our result is already in an appropriately named variable.
Technically, initializing from cin is probably trivially faster (given it avoids an unnecessary comparison in the first loop where initializing to -HUGE_VAL requires that test), but that's almost certainly meaningless relative to all the other overhead involved (e.g. reading from a file handle and parsing it).
You have to add more information for this question to be answered appropriately, but in this little code snippet, largest is acting as a kind of starting point for comparison between user input and the by now largest input.
The code transfers the input value to the variable largest, provided it is larger than the previously input values.
But this cannot work with the first value, as there was no previous. The fix is to assign the first value to largest unconditionally. (This is a correct measure, as the largest among a list of a single value is that value.)
A variant is to initialize largest with the smallest possible value.
int main ()
{
double largest= std::numeric_limits<double>::lowest();
double input;
while (cin >> input)
{
if (input > largest)
{
largest = input;
}
}
}
There is a slight difference between the two versions: the first will require at least one value before it can exit. The second does not, and in case no value is input, it will return the smallest possible double.
I'm working on an algorithm and I need to initialize the vector of ints:
std::vector<int> subs(10)
of fixed length with values:
{-inf, +inf, +inf …. }
This is where I read that it is possible to use MAX_INT, but it's not quiete correct because the elements of my vector are supposed to be greater than any possible int value.
I liked overrloading comparison operator method from this answer, but how do you initialize the vector with infinitytype class objects if there are supposed to be an int?
Or maybe you know any better solution?
Thank you.
The solution depends on the assumptions your algorithm (or the implementation of your algorithm) has:
You could increase the element size beyond int (e.g. if your sizeof(int) is 4, use int64_t), and initialize to (int64_t) 1 + std::numeric_limits<int>:max() (and similarly for the negative values). But perhaps your algorithm assumes that you can't "exceed infinity" by adding on multiplying by positive numbers?
You could use an std::variant like other answers suggest, selecting between an int and infinity; but perhaps your algorithm assumes your elements behave like numbers?
You could use a ratio-based "number" class, ensuring it will not get non-integral values except infinity.
You could have your algorithm special-case the maximum and minimum integers
You could use floats or doubles which support -/+ infinity, and restrict them to integrality.
etc.
So, again, it really just depends and there's no one-size-fits-all solution.
AS already said in the comments, you can't have an infinity value stored in int: all values of this type are well-defined and finite.
If you are ok with a vector of something working as an infinite for ints, then consider using a type like this:
struct infinite
{ };
bool operator < (int, infinite)
{
return true;
}
You can use a variant (for example, boost::variant) which supports double dispatching, which stores either an int or an infinitytype (which should store the sign of the infinity, for example in a bool), then implement the comparison operators through a visitor.
But I think it would be simpler if you simply used a double instead of int, and whenever you take out a value that is not infinity, convert it to int. If performance is not that great of an issue, then it will work fine (probably still faster than a variant). If you need great performance, then just use MAX_INT and be done with it.
You are already aware of the idea of an "infinite" type, but that implementation could only contain infinite values. There's another related idea:
struct extended_int {
enum {NEGINF, FINITE, POSINF} type;
int finiteValue; // Only meaningful when type==FINITE
bool operator<(extended_int rhs) {
if (this->type==POSINF) return false;
if (rhs.type==NEGINF) return false;
if (this->type==FINITE && rhs.type==POSINF) return false;
if (this->type==NEGINF && rhs.type==FINITE) return false;
assert(this->type==FINITE && rhs.type==FINITE);
return this->finiteValue < rhs.finiteValue)
}
// Implicitly converting ctor
constexpr extended_int(int value) : type(FINITE), finiteValue(value) { }
// And the two infinities
static constexpr extended_int posinf;
static constexpr extended_int neginf;
}
You now have extended_int(5) < extended_int(6) but also extended_int(5) < extended_int::posinf
This might seem like a weird question, but how would I create a C++ function that tells whether a given C++ function that takes as a parameter a variable of type X and returns a variable of type X, is injective in the space of machine representation of those variables, i.e. never returns the same variable for two different variables passed to it?
(For those of you who weren't Math majors, maybe check out this page if you're still confused about the definition of injective: http://en.wikipedia.org/wiki/Injective_function)
For instance, the function
double square(double x) { return x*x};
is not injective since square(2.0) = square(-2.0),
but the function
double cube(double x) { return x*x*x};
is, obviously.
The goal is to create a function
template <typename T>
bool is_injective(T(*foo)(T))
{
/* Create a set std::set<T> retVals;
For each element x of type T:
if x is in retVals, return false;
if x is not in retVals, add it to retVals;
Return true if we made it through the above loop.
*/
}
I think I can implement that procedure except that I'm not sure how to iterate through every element of type T. How do I accomplish that?
Also, what problems might arise in trying to create such a function?
You need to test every possible bit pattern of length sizeof(T).
There was a widely circulated blog post about this topic recently: There are Only Four Billion Floats - So Test Them All!
In that post, the author was able to test all 32-bit floats in 90 seconds. Turns out that would take a few centuries for 64-bit values.
So this is only possible with small input types.
Multiple inputs, structs, or anything with pointers are going to get impossible fast.
BTW, even with 32-bit values you will probably exhaust system memory trying to store all the output values in a std::set, because std::set uses a lot of extra memory for pointers. Instead, you should use a bitmap that's big enough to hold all 2^sizeof(T) output values. The specialized std::vector<bool> should work. That will take 2^sizeof(T) / 8 bytes of memory.
Maybe what you need is std::numeric_limits. To store the results, you may use an unordered_map (from std if you're using C++11, or from boost if you're not).
You can check the limits of the data types, maybe something like this might work (it's a dumb solution, but it may get you started):
template <typename T>
bool is_injective(T(*foo)(T))
{
std::unordered_map<T, T> hash_table;
T min = std::numeric_limits<T>::min();
T max = std::numeric_limits<T>::max();
for(T it = min; i < max; ++i)
{
auto result = hash_table.emplace(it, foo(it));
if(result.second == false)
{
return false;
}
}
return true;
}
Of course, you may want to restrict a few of the possible data types. Otherwise, if you check for floats, doubles or long integers, it'll get very intensive.
but the function
double cube(double x) { return x*x*x};
is, obviously.
It is obviously not. There are 2^53 more double values representable in [0..0.5) than in [0..0.125).
As far as I know, you cannot iterate all possible values of a type in C++.
But, even if you could, that approach would get you nowhere. If your type is a 64 bit integer, you might have to iterate through 2^64 values and keep track of the result for all of them, which is not possible.
Like other people said, there is no solution for a generic type X.
I wrote a recursive function that computes the sum of an array of double. For some reasons, the value returned by my recursive function is not correct. Actually, my recursive sum does not match my iterative sum. I know I made a little mistake somewhere, but I can't see where. Your help will be very appreciated. I only pasted the recursive function. I am using C++ on Visual Studio. Thanks!
double recursive_sum(double array_nbr[], int size_ar)
{ double rec_sum=0.0;
if( size_ar== 0)
return -1;
else if( size_ar> 0)
rec_sum=array_nbr[size_ar-1]+recursive_sum(array_nbr,size_ar-1);
return rec_sum;
}
//#### Output######
The random(s) number generated in the array =
0.697653 | 0.733848 | 0.221564 |
Recursive sum: 0.653066
Iterative sum: 1.65307
Press any key to continue . . .
Well, because sum of no elements is zero, not minus one.
if (size_ar == 0.0)
return 0.0;
Think about it this way: sum(1,2,3) is the same as sum(1,2) + sum(3) just as it is the same as sum(1,2,3)+sum() — in all three cases, you add 1, 2, and 3 together, just in a slighlty different ways. That's also why the product of no elements is one.
Try changing "if( size_ar== 0) return -1;" to return 0.
While this does not account for the large discrepancy in your output, another thing to keep in mind is the ordering of operations once you have fixed the issue with returning a -1 vs. 0 ... IEEE floating point operations are not necessarily commutative, so make sure that when you are doing your recursive vs. iterative methods, you add up the numbers in the exact same order, otherwise your output may still differ by some epsilon value.
For instance, currently in your recursive method you're adding up the values from the last member of the array in reverse to the first member of the array. That may, because of the non-commutative property of floating point math, give you a slightly different value (small epsilon) than if you sum up the values in the array from first to last. This probably won't show on a simple cout where the floating point values are truncated to a specific fixed decimal position, but should you attempt to use the == operation on the two different summations without incorporating some epsilon value, the result may still test false.
If you have a function int getMin(int a[], int n) then what would you say is the cleanest solution to deal with the empty array case?
Return a pointer to the minimum element instead of the element itself. This way, a pointer value of one past the end of the array can indicate not found. (Or in this case empty)
This is the strategy taken by std::min_element, which already implements what you're doing.
You can even implement this in terms of std::min_element:
int* getMin(int a[], int n)
{
return std::min_element(a, a+n);
}
Assuming you're looking for the minimum value in the array how about:
if (!n)
throw YourPreferredException();
Or:
#include <limits>
//...
if (!n)
return std::numeric_limits<int>::max();
Or, if it should never happen:
#include <cassert>
//...
assert(n);
It depends on the application and the values you're expecting to be passing in. What makes most sense and what fits the existing code base is hard to guess.
Maybe instead, do it the way the standard library does: Take two iterators as parameters and return the end parameter if the sequence is empty. Better still use min_element instead of rolling your own.
If you need to do it the array/length way either throw or return std::numeric_limits<int>::max()
There is definitely no "cleanest solution" absent an understanding of the domain. Mathematically, the infimum of any set of values from a domain is the greatest lower bound (in the domain) of all elements of the set. For the extended integers, this would be +infinity for an empty set. (See, e.g., the Wikipedia article on Empty Set) If your domain is all C++ int values, a (mathematically consistent) return value would then be INT_MAX.