I'm having trouble using
Indexed[]
with
NIntegrate[]
in Mathematica. I want to evaluate something like
NIntegrate[Indexed[t, 1], {Indexed[t, 1], 0, 1}]
with a lot more variables (indices). However, I receive this error:
"Tag Indexed in Indexed[t,1] is Protected"
I'm fairly certain the problem arises from the second argument because
NIntegrate[Indexed[x, 1]^2 Indexed[x, 2]^2, x \[Element] Rectangle[]]
evaluates as I would expect.
I have tried unprotecting the second argument and wrapping it with an evaluate and neither worked. I think I need to use
Indexed[]
because I have a complicated expression over a complicated region with a variable number of dimensions. I have to use
NIntegrate[]
because
Integrate[]
failed to solve my integral, but otherwise performed as I expected with, for example,
Integrate[Indexed[t, 1], {Indexed[t, 1], 0, 1}].
I am using Mathematica 10.0.
It is not clear what is the purpose of Indexed in your case.
If that is just for the decoration I would suggest using Subscript instead of Indexed. Try this
NIntegrate[Subscript[t, 1], {Subscript[t, 1], 0, 1}]
It works on Mathematica 10.0
Related
I'm trying to sort a list of lists by the third element in the sub-lists, and am doing so using a predicate for sorting defined as such:
sortData(X, [_,_,Z1], [_,_,Z2]) :- compare(X, Z1, Z2).
When I try to use this predicate in my Prolog console (predsort(sortData, [[0,5,2],[6,3,1]], X).), I get an error stating:
uncaught exception: error(existence_error(procedure,predsort/3),top_level/0)
I did some research and found this SO question on the matter, but was unable to use the tips suggested by the commenters to fix my error. I tried adding in :- use_module(library(sort)). to my code, but that gave a warning upon compilation (warning: unknown directive use_module/1 - maybe use initialization/1 - directive ignored).
Any tips as to why I may be getting the predsort/3 existence error?
EDIT: My desired output given the call above is: X = [[6, 3, 1], [0, 5, 2]].
The Julia style guide says that functions which "modify their arguments" should have their name end on a !.
However, what about:
functions that do modify their arguments, but return them to their original state before returning?
functions that return a Task which modifies the argument when executed?
functions that return such a Task, but when it is done, the arguments will have been restored to their original states?
Should their names end on a !?
As an example, consider this module for finding exact covers using Knuth's Dancing Links Algorithm. It implements a type CoverSet that can be populated with the subsets and then queried for the first exact cover:
set = CoverSet()
push!(set, [1, 2])
push!(set, [2, 3])
push!(set, [3, 4])
push!(set, [4, 1])
find_exact_cover(set) # returns [1, 3]
The find_exact_cover function temporarily modifies the data in set while searching for a solution, but by the time find_exact_cover returns, set will be in its original state. Should it be named find_exact_cover! instead?
Similarly, exact_cover_producer returns a Task that produces all exact covers, but when that Task is done, set will have been restored:
for cover in exact_cover_producer(set)
println(cover) # prints [1,3] and [2,4]
end
# By now, set is restored.
Should it be exact_cover_producer!?
I am aware that this might be considered subjective, so let me clarify what I am asking for: I would like to know whether there is a convention on this in the Julia community, and ideally also examples from the standard library or any packages that use either style.
See e.g. the discussion at Julia commit e7ce4cba44fa3b508fd50e0c3d03f6bc5a7a5032: the current convention is that a function is mutating and hence has a ! appended if it changes what one of its arguments would be == to.
(But there is some justification for slightly broader definitions as well; see the abovementioned discussion.)
I am learning Ruby. I have 2 arrays in which I need to get elements from one or the other in coordinated fashion. The elements are each objects of the same class.
Using the Array.each_with_index method does get elements from the first array. Now, based upon the content of the object found, I need to get objects at various places in the second array. if I use notation like:
x = arrayName[index]
It converts x into an array, and the wanted array object is not returned. I suppose if I convert the array into a hash using the subscripts as keys, it might work, but that is UGLY. I am using Windows 7 and Ruby 1.9.3. The larger question is: Is this correct behavior? Elsewhere it clearly states that expression arrayName[index] returns the object at the index. It was a merry chase to discover this, but it seems to clearly be repeatable. Thanks much for any light.
I think what you're looking for is something along the lines of this.
arr_1 = ["a", "b", "c"]
arr_2 = [1, 2, 3]
arr_1.zip(arr_2)
#=>[["a", 1], ["b", 2], ["c", 3]]
There are variations on this to get what I think you're going for. You can use {item => arr_2[i]} instead of the block I used above to have them be nested hashes.
Does anybody know how I can use the Manipulate command with the show command.
Basically, I want to display multiple funcitons on one coordinate system. However, I want only one of them to be "manipulated" (i.e. other should be static).
I can not sort out how to use Show and Manipulate together.
Thanks for your help!
HR
If you don't want the manipulated variable, a, in one of the plots, simply omit it.
Manipulate[
Show[
Plot[a x, {x, 0, 3.5}],
ListPlot[{1, 2/a, 3/a}]],
{{a, 1}, 0, 2}]
Is there any way to get at the actual messages generated during the evaluation of an expression in Mathematica? Say I'm numerically solving an ODE and it blows up, like so
In[1] := sol = NDSolve[{x'[t] == -15 x[t], x[0] == 1}, x, {t, 0, 1},
Method -> "ExplicitEuler"];
In this case, I'll get the NDSolve::mxst error, telling me the maximum number of 10000 steps was reached at t == 0.08671962566152185. Now, if I look at the $MessageList variable, I only receive the message name; in particular, the information about the value of t where NDSolve decided to quit has been lost.
Now, I can always get that information from sol using the InterpolatingFunctionDomain function from one of the standard add-on packages, but if I can somehow pull it out of the message, it would be quite helpful.
You might be able to use $MessagePrePrint to set up a function which would store away each of the messages for later retrieval.
I don't know if this will work, but if the only thing you want to know are the values of specific parameters at the point of error then a kludgy way of getting them would be to define those variables with dummy values globally. This works with loop counters, but I don't know if it works from within NDSolve. Another kludge would be to make t Dynamic and have an evaluated cell with t.
A more elegant (and probably the correct) approach would be to use Reap and Sow.