Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have to make a function like this:
success :: (Int,Int,Int) -> Int -> (Int,Int,Int) -> Bool
(Int,Int,Int) First three Ints are attributes (that means numbers from 1 to 20)
Int in the middle is the one to compare the differences between attributes and dices.
(Int,Int,Int) the last three Ints are diced by a dice with 20 sides for each of them.
sucess (attribute 1,attribute 2, attribute 3) -> Compare difference between attributes and dices -> (Dice 1,Dice 2,Dice 3) -> Bool
Now I have to compare attribute 1 and dice 1 and so on.
If the dice is higher then the attribute, I've to notice the difference.
If lower I've to notice 0. If the three differences are higher than the difficult I lose
=> function is false
For example:
success (16,13,8) 4 (1,17,10) => false difference is 6
success (16,13,8) 4 (1,10, 9) => true difference is only 1!
I am a newby at haskell-programming and have no idea how to handle this.
This looks like a homework assigment, you're supposed to do it by yourself. So I will offer some hints instead of the full working solution.
You could define that function by using pattern matching:
success (a1, a2, a3) d (d1, d2, d3) = ... -- put condition at here
where diff1 = ...
diff2 = ...
diff3 = ...
I believe you can fill in the missing details quite easily.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed last year.
Improve this question
I need help with understanding the shaping theorem for MDPs. Here's the relevant paper: https://people.eecs.berkeley.edu/~pabbeel/cs287-fa09/readings/NgHaradaRussell-shaping-ICML1999.pdf it basically says that a markov decision process that has some reward function on transitions between states and actions R(s, a, s') has the same optimal policy as a different markov decision process with it's reward defined as R'(s, a, s') = R(s, a, s') + gamma*f(s') - f(s), where gamma is the time-discount-rate.
I understand the proof, but it seems like a trivial case where it breaks down is when R(s, a, s') = 0 for all states and actions, and the agent is faced with the path A -> s -> B versus A -> r -> t -> B. With the original markov process we get an EV of 0 for both paths, so both paths are optimal. But with the potential added to each transition we get, gamma^2*f(B)-f(A) for the first path, and gamma^3*f(B) - f(A) for the second. So if gamma < 1, and 0 < f(B), f(A), then the second path is no longer optimal.
Am I misunderstanding the theorem, or am I making some other mistake?
You are missing the assumption that for every terminal, and starting state s_T, s_0 we have f(s_T) = f(s_0) = 0. (Note, that in the paper there is an assumption that after terminal state there is always the new starting state, and the potential "wraps around).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is it possible to load-linked a register to itself?
There is a line of MIPS(R2000) assembly code in my textbook says:
ll $r2, 0($r2)
Is this correct?
If it's correct: Is this instruction used when the original content in $r2 is not important but one only want to store a value into $r2 and he doesn't want others to store at the same time?
LL does not prevent anyone from storing to the address that is loaded, it only enables checking if someone did.
The LL instruction you posted looks legal to me. Whether it would make sense at that point depends on what else the program does.
It is possible that the address is also stored somewhere else and later read from someplace else for a call to SC.
Modified example from the reference manual:
L1:
ADD T1, ZERO, T0 # copy T0 -> T1
LL T1, (T1) #
ADDI T2, T1, 1 # increment
SC T2, (T0) # try to store, checking for atomicity
BEQ T2, 0, L1 # if not atomic (0), try again
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have to matrices A(32*32) and B(32*n), in which 'n' is coming from inputs and is between 2000 to 2000000.
I have two kind of inputs one is integers between 0 to 255 and the other one is 0,1. this multiplication is in a loop that iterates 3000 times. B(32*n) comes form input and is constant in all of the iterations but A(32*32) can change in each iteration.
//read B from file
//read A from file
double D[3000];
for(int i = 0; i < 3000; i++)
{
C = multiply(A, B);
// D[i] = mean of all elements in C
// build A from B using D[i] (this part is really complicated sequential process that contains lots of if and switches)
}
What is the fastest way to do this?
thank you.
Nobody here is going to write code for you, that is not what Stack Overflow is intended for. However, it would appear to be that there are a number of characteristics of the problem which you should be looking to exploit to improve the performance of your code:
Recognise that because one of the matrices only contains 0 or 1 and you are performing this in integer, what you are describing as matrix multiplication is really a large number of independent sparse sums
Recognise that because the next operation is to compute an average, you don't actually have to store the intermediate dot products and could directly perform a reduction on partial results of the matrix row summation
There are probably parallel primitives in the thrust library which you could use for prototyping, and an optimal hand written kernel would be aiming to fuse both the first and most of the second part of the operation into a single kernel.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to write a function in Haskell that can solve the following problem(physic_problem):
What is the height (in a whole number of meters) of the shortest building that you could drop a ball from such that it would take at least 5 seconds to hit the ground?
The equation can be found here http://en.wikipedia.org/wiki/Equations_for_a_falling_body
I really tried hard on this and i need help!
Thank you so much!
Consider for instance this,
g :: Double
g = 9.81
dist :: Double -> Double
dist t = g * t^2 / 2
Then,
> dist 5
122.625
Additionally you may want to create a module out of the equations in that Wiki.
Update
For delivering an integral value consider for example
dist' :: (Integral a) => Double -> a
dist' t = ceiling $ g * t^2 / 2
Here we use ceiling (upper bound), yet note Converting to Integral for other rounding functions. Hence,
> dist' 5
123
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would you define a total order? For example, if you needed to define a total ordering or a shape, etc. How would you go about doing so?
Edit: Specifically, how would you define a total order based on an object with the coordinates (x,y,z). I don't understand how you could structure an ordering whereby each object is unique and sortable.
There is no "natural" ordering on 2D or 3D objects. However, if you want to induce an ordering, you can compare them by their coordinates, for example this way:
// returns -1 if o1<o2, 1 if o1>o2, 0 if o1==o2
int Compare(MyObject o1 ,MyObject o2)
{
if(o1.x>o2.x) return 1;
if(o1.x<o2.x) return -1;
if(o1.y>o2.y) return 1;
if(o1.y<o2.y) return -1;
if(o1.z>o2.z) return 1;
if(o1.z<o2.z) return -1;
return 0;
}
This assumes objects are uniquely identified by their coordinates, of course.
This ordering will let you sort and compare such objects. The question you have to answer yourself is if it helps you for any of the problems you want to solve with that. An ordering on a 1D-set is typically used to make lookups faster, especially when you want not only a specific element from your set, but all elements from a given range.
For 2D or 3D sets, a similar question is to find all element sets within a given rectangle or cube. For that purpose, the order above does not support you very well. There are datastructures like a 2D quadtree or 3D octree supporting this task much better.