I'm having trouble finding a way to come across this problem...I have turtles creating a social-network (I use undirected links).
Agents have many variables, among which "y" and "ro". What I would like to know is the mean [ro] of the turtles connected with turtles with [y < 1]...is there a way?
In practice...in the following formula
mean [ro] of turtles with [link-neighbor? turtle n]
I would like instead of turtle n an agent-set turtles with [y < 1]...is there a way?
thanks!
turtles-own [y ro]
to test
ca
crt 50 [
set y random-float 2
set ro one-of [1 2]
]
ask turtles [
create-links-with n-of 5 other turtles
]
show mean [ro] of (turtles with [has-poor-neighbor])
end
to-report has-poor-neighbor ;turtle proc
report any? link-neighbors with [y < 1]
end
Related
I would like Mathematica to evaluate square root of a squared variable. Instead it is just returning the squared variable under square root. I wrote a simple code as an example:
x = y^2
z = FullSimplify[Sqrt[x]]
But it is returning y^2 under a square root sign!
This behavior is documented on the Sqrt reference page:
Sqrt[z^2] is not automatically converted to z.
[…]
These conversions can be done using PowerExpand, but will typically be correct only for positive real arguments.
Thus:
In[1]:= x = y^2
Out[1]= y^2
In[15]:= PowerExpand[Sqrt[x]]
Out[15]= y
You can also get simplifications by supplying various assumptions:
In[10]:= Simplify[Sqrt[x], Assumptions -> Element[y, Reals]]
Out[10]= Abs[y]
In[13]:= Simplify[Sqrt[x], Assumptions -> y > 0]
Out[13]= y
In[14]:= Simplify[Sqrt[x], Assumptions -> y < 0]
Out[14]= -y
If you want more help, I suggest asking on the Mathematica Stack Exchange.
I get a matrix A with the shape [ M x N x L1 ] and another matrix B with shape [ M x L2 ], L1 and L2 have the same dimension size but with different values. The M in A and B are corresponding.
For every dimension in A[M,:,:], I want to give them the value B[M:]. That's to say, I want use L2 in matrix B[M] to initialize every L1 in A[M,:], how can I operate this in tensorflow?
Details: Suppose A is a matrix that contains M sentences with N words that have L1 dimension word embeddings. B is a matrix contains M sentences with a score in L2 dimension. len(L1)=len(L2).
How can I replace every word embedding in the sentence with the score in matrix B?
Thanks for any help
I figured out today morning.
One possible way is:
As M&N are None dim in tensorflow, A: [None, None, L], B: [None, L], so I used tf.transpose()
what we should do is :
1, swift dimensions:
tf.transpose(A,[1,0,2]): [ M x N x L1 ] ===> [ N x M x L1 ]
A ------> A_
2,initialize all the A_ matrix with 1 value
change the values to 1:
A_ ====> _A_
3, mul the matrixs:
tf.mul(_A_, B) ===> C
4,Then swift back the dim:
tf.transpose(**C, [1,0,2]**)
In OWL:
There is a class X and the properties P1, P2, and P3, each of which has domain X.
I want to say:
Every instance of X must at least participate in a relation with one of the properties P1 or P3.
Every instance of X which participates in a relation with P2 must also participate in a relation with P1.
But every instance of X may only participate in relations with P1 and P2 or in relations with P3.
Maybe it is easier to understand with some syntax and labels:
:Chronology a owl:Class ;
rdfs:label "X" ;
:hasBegin a owl:DatatypeProperty ;
rdfs:label "P1" ;
rdfs:domain :Chronology .
:hasEnd a owl:DatatypeProperty ;
rdfs:label "P2" ;
rdfs:domain :Chronology .
:hasNoBeginNoEnd a owl:DatatypeProperty ;
rdfs:label "P3" ;
rdfs:domain :Chronology .
I understand the concept of anonymous classes and restrictions but nothing really seems to fit.
You have a few different constraints here, but they can all be represented in OWL. Let us address the constraints one at a time.
Every instance of X must at least participate in a relation with one of the properties P1 or P3.
This says that for every x, either there is a y such that P1(x,y), or there is a z such that P2(x,z). In OWL, this is expressed by
X SubClassOf ((P1 some Thing) or (P2 some Thing))
The class expression P1 some Thing represents the class of things that are related by P1 to some entity. Similarly for P2 some Thing. The subClassOf axiom as a whole says that “if something is an X, then it is either a P1 some Thing or a P2 some Thing.” (You could also use P min 1 instead of P some Thing, if you wanted to. It is not a significant difference.)
Every instance of X which participates in a relation with P2 must also participate in a relation with P1.
This says that for every x, if there is a y such that P2(x,y), then there is also a z such that P1(x,z). Another way of saying this is that “for every x, if x is an X and there is a y such that P2(x,y), then there is also a z such that P1(x,z).” This can be expressed by another subclass axiom:
(X and (P2 some Thing)) SubClassOf (P1 some Thing)
(For the sake of generality, I used X and (P2 some Thing) on the left side of this subclass axiom. In this specific case, since X is the domain of P2, we can infer that P2 some Thing is a subclass of X, so we could also have used just P2 some Thing on the left.)
But every instance of X may only participate in relations with P1 and P2 or in relations with P3.
This says that if some x is an X and there is a y such that P3(x,y), then there is no z such that P1(x,z) or P2(x,y), and vice versa. You can represent this in a few ways. You could use two subclass axioms:
(X and (P3 some Thing)) SubClassOf ((P1 max 0) and (P2 max 0))
(X and ((P1 some Thing) or (P2 some Thing))) SubClassOf (P3 max 0)
You could also use a single disjoint class axiom (notice that X appears on both sides)
(X and (P3 some Thing)) DisjointWith (X and ((P1 some Thing) or (P2 some Thing)))
(As I noted in the previous case, since the domain of the properties is X, the class X and (P3 some Thing) is equivalent to P3 some Thing. The left side of these subclass axioms could also be simply P3 some Thing and (P1 some Thing) or (P2 some Thing), and the classes in the disjoint axioms could be P3 some Thing and (P1 some Thing) or (P2 some Thing).)
Here's what the classes and axioms in the ontology looks like (in the N3 format):
:X a owl:Class ;
rdfs:subClassOf
[ a owl:Class ;
owl:unionOf ([ a owl:Restriction ;
owl:onProperty :P1 ;
owl:someValuesFrom owl:Thing
] [ a owl:Restriction ;
owl:onProperty :P2 ;
owl:someValuesFrom owl:Thing
])
] .
[] a owl:Class ;
rdfs:subClassOf
[ a owl:Restriction ;
owl:onProperty :P1 ;
owl:someValuesFrom owl:Thing
] ;
owl:intersectionOf (:X [ a owl:Restriction ;
owl:onProperty :P2 ;
owl:someValuesFrom owl:Thing
]) .
[] a owl:Class ;
owl:disjointWith
[ a owl:Class ;
owl:intersectionOf (:X [ a owl:Restriction ;
owl:onProperty :P3 ;
owl:someValuesFrom owl:Thing
])
] ;
owl:intersectionOf (:X [ a owl:Class ;
owl:unionOf ([ a owl:Restriction ;
owl:onProperty :P1 ;
owl:someValuesFrom owl:Thing
] [ a owl:Restriction ;
owl:onProperty :P2 ;
owl:someValuesFrom owl:Thing
])
]) .
Commentary on Blank Node Usage
As pointed out in the comments, the ontology above uses class expressions represented by blank nodes as the subjects of the two "general class axioms", i.e., the subclass axioms that related two class expressions, neither of which is a simple class identifier. The original OWL Web Ontology Language
Reference includes, in Appendix E: Rules of Thumb for OWL DL Ontologies:
Avoid orphan blank nodes
In general, blank nodes occurring in the graph either represent unnamed individuals, or should be exactly one of the following:
The object of an rdfs:subClassOf, owl:equivalentClass, owl:disjointWith, owl:someValuesFrom, owl:allValuesFrom or rdf:type triple.
The subject of an rdf:type triple with object owl:AllDifferent.
An element in an rdf:List.
Orphan blank nodes, i.e. those which are not the object of a triple are, in general, not allowed (other than the owl:AllDifferent case described above).
At first glance, it would seem that the ontology provided above violates this, because the "general class axioms" (class axioms whose subject is not a class identifier) have class expressions as their subject. However, section 3.2 Class Axioms gives the syntax of, e.g., rdfs:subClassOf axioms as
AXIOM SCHEMA: class description rdfs:subClassOf class description
That section also includes the note:
NOTE: In OWL Lite the subject of an rdfs:subClassOf statement must be a class identifier. The object must be either a class identifier or a property restriction.
This suggests that Appendix E is mistaken in omitting certain cases where "orphan blank nodes" are allowed. That suggestion isn't normative, of course; it opens with the introduction:
The following rules give an informal characterization of the conditions for an RDF graph to be a DL ontology. This is not intended to replace the characterization given in S&AS, but instead gives some general pointers — the idea is that if you stick to these guidelines, you're more likely to produce OWL DL ontologies. [emphasis added]
For confirmation, we need to look at section 8. OWL Full, OWL DL and OWL Lite, which describes the precise constructs that are allowed in OWL Full, OWL DL, and Owl Lite ontologies. That section reiterates that, in OWL Lite,
the subject of rdfs:subClassOf triples be class names and the object of rdfs:subClassOf triples be class names or restrictions;
but puts no such restrictions on OWL DL ontologies. Section 8 does require, for OWL DL ontologies, that
All axioms must be well-formed, with no missing or extra components, and must form a tree-like structure. The last constraint implies that all classes and properties that one refers to are explicitly typed as OWL classes or properties, respectively.
This points out that
[] rdfs:subClassOf :Foo .
is not valid OWL DL, but that
[] a owl:Class ;
rdfs:subClassOf :Foo .
is (provided that :Foo is an owl:Class, of course). The non-normative Appendix E simply missed a case where "orphan blank nodes" can be used. General class axioms don't get used all that often, except when some particularly complicated sentences need to be represented, so it's not a hard mistake to make.
For some more information about general class axioms, see Being complex on the left-hand-side: General Concept Inclusions.
I asked this question yesterday but not sure if I made clear what I was looking for. Say I have two curves defined as f[x_]:=... and g[x_]:=... as shown below. I want to use Mathematica to determine the abscissa intersection of the tangent to both curves and store value for each curve separately. Perhaps this is really a trivial task, but I do appreciate the help. I am an intermediate with Mathematica but this is one I haven't been able to find a solution to elsewhere.
f[x_] := x^2
g[x_] := (x - 2)^2 + 3
sol = Solve[(f[x1] - g[x2])/(x1 - x2) == f'[x1] == g'[x2], {x1, x2}, Reals]
(* ==> {{x1 -> 3/4, x2 -> 11/4}} *)
eqns = FlattenAt[{f[x], g[x], f'[x1] x + g[x2] - f'[x1] x2 /. sol}, 3];
Plot[eqns, {x, -2, 4}, Frame -> True, Axes -> None]
Please note that there will be many functions f and g for which you won't find a solution in this way. In that case you will have to resort to numerical problem solving methods.
You just need so solve a system of simultaneous equations:
The common tangent line is y = a x + b.
The common slope is a = f'(x1) = g'(x2)
The common points are a x0 + b = f(x0) and a x1 + b = g(x1).
Depending on the nature of the functions f and g this may have no, one, or many solutions.
With Mathematica 8.0.1.0, I have used FindRoot[] to identify the intersection of two 2 pdf functions.
But if I have pdf functions that intersect at more than one point, and I have the upper limit of the x axis range beyond the second intersection, FindRoot[] only returns the second intersection.
pdf1 = 1/x 0.5795367855565214` (E^(
11.170058830053032` (-1.525439351903338` - Log[x]))
Erfc[1.6962452696714152` (-0.5548887795964352` - Log[x])] +
E^(1.2932713057519` (2.60836043407439` + Log[x]))
Erfc[1.6962452696714152` (2.720730943938539` + Log[x])]);
pdf2 = 1/x 0.4648445097126269` (E^(
5.17560914275408` (-2.5500941338198615` - Log[x]))
Erfc[1.7747318880142482` (-2.139288893723375` - Log[x])] +
E^(1.1332542415053757` (3.050849516581922` + Log[x]))
Erfc[1.7747318880142482` (3.1407996592474956` + Log[x])]);
Plot[{pdf1, pdf2}, {x, 0, 0.5}, PlotRange -> All] (* Shows 1st intersection *)
Plot[{pdf1, pdf2}, {x, 0.4, 0.5}, PlotRange -> All] (* Shows 2nd intersection *)
{x /. FindRoot[pdf1 == pdf2, {x, 0.00001, 0.5}],
x /. FindRoot[pdf1 == pdf2, {x, 0.00001, 0.4}]}
The above plots show the issue. When plotted they intersect at two points:
{0.464719, 0.0452777}
respectively.
As I can't know before hand if I'll have a second intersection and I don't know where it might fall on the x axis if I did, can anyone suggest a way to have FindRoot[] only return the first intersection rather than the second?
If not, can anyone suggest another way to go about it?
With FindRoot[], you can only get a single root for a given starting point. Iterating through different options is cumbersome and you might not even get the desired result for certain edge cases unless you hit upon the right choice of starting point.
In this case, something like NSolve or Reduce might be a better option. If you know that your expressions decay, using a reasonable upper bound for possible values of x, you can use the following, which is pretty quick and will give you all roots.
NSolve[{pdf1 == pdf2, 0 < x < 1}, x] // Timing
Out[1]= {0.073495, {{x -> 0.0452777}, {x -> 0.464719}}}
How about the following:
First you have to find all roots in one step. I do this with
roots=Reduce[pdf1==pdf2&&0.000001<x<0.5,x]
And then you could take the minimum (first root on the x axis in your special case).
rootMin=Min[N[x/.{ToRules[roots]}]]