A predicate which holds for at most 2 - verification

I want to write a predicate which holds for at most two items.
I tried doing one x,y: Object |
However this did not give the desired result
I then tried one x,y: Object | x != y
However this gave no instances
I was thinking of something along the lines of
some ..
{
one..
}
Any help would be appreciated

one forces there to be only one atom, but x, y is two. One way to do this would be some disj x, y: Object | no z: Object - x - y | ...

Related

Tensorflow, how to preserve the intermediate node value and reuse them

Assuming I have the following graph.
- X3 and Z are the values I care about.
- X and Y are inputs. In each different. iteration, the coming values and shapes of X and Y are different, so I think they much be placeholder
- The circumstance is that I need to run this graph twice in different time point to get X3 and Z asynchronously.
+---+ op: +1 op: *3
| X +------------> X_1 +-----------> X3 +---+
+---+ + + | Y |
| | +-+-+
| op:add |
| | |
| | |
| op: add v op:add |
+-------------> <------------+
Z
At early time point, I get an input X(say X=7 and I don't know what's Y is at this moment). I want to see the value of X3. So I execute sess.run([X3], {X:7}), then it returns 24 as expected.
At later time point, I get another input Y(say Y=8), and this time I only want to take a look at node Z. But the point is that I have to execute sess.run([Z], {X:7, Y:8}) to get the result.
The problem is, for the later run, I have to feed X again to recalculate the intermediate node X_1 and X3. It calculates flow X--> X_1 --> X3 twice which hurts the efficiency.
My idea is that X_1 and X3 will contain values(X_1=8,X3=24) after the early run until the graph is destroyed, then I can directly leverage the instead of recalculating.
Is there a way to achieve the goal?
The following doesn't solve your problem completely, but it gets away with feeding X again:
X_temp = tf.Variable(0, dtype=tf.int32)
X = tf.placeholder_with_default(X_temp, shape=())
Y = tf.placeholder(tf.int32, shape=())
X_temp = tf.assign(X_temp, X)
X_1 = X_temp + 1
X3 = X_1 * 3
Z = X_1 + X3 + Y
sess = tf.InteractiveSession()
print(sess.run(X3, {X:7}))
print(sess.run(Z, {Y:8}))
#24
#40
One option I would recommend is:
temp_X1, temp_X3 = sess.run([X_1, X3], feed_dict={X:7})
sess.run(Z, feed_dict={X_1:temp_X1, X3:temp_X3, Y: 8}
You don't need to store everything inside the tf graph.
See the tensorflow doc for other options (like using a Saver, etc.)
Note: Feeding into placeholder is recommended by the doc, but feeding into an intermediate Tensor achieves your requirement the easiest.

Clustering method. Choice variables when sum of variables is 1 on each observation

There are 3 variables are x, and z=1-(x+y). ( x>=0, y>=0, z>=0 )
Data are like below.
O(1)= (x1,y1,z1)
O(2)= (x2,y2,z2)
...
O(n)= (xn,yn,zn)
I thought that z is not necessary to express each observation because z is determined by x and y.
So, I did clustering this data with x and y.
And also did clustering same data with x, y and z, too.
The results are different.
Because the distances in 2D and 3D are not equal. (It changed.)
(Yes, Of course, the distances 2D and 3D are not equivalent.)
But, What is the right way do clustering in this case?
Do I have to use x and y? Or, do I have to use x, y and z? Why?
Please someone help me. Thank you in advance!
Below is R code.
############
x <- sample(c(0:100), 100, replace = T)
y <- sample(c(0:100), 100, replace = T)
z <- 200 - (x+y)
xyz <- cbind(x,y,z)
xyz <- xyz/200 # z=1-(x+y)
xy <- zyz[,-3]
require(fpc)
(xy.pamk <- pamk(xy))
plot(xy,col=xy.pamk$pamobject$clustering)
(xyz.pamk <- pamk(xyz))
require(rgl)
plot3d(xyz,col=xyz.pamk$pamobject$clustering,xlim=c(0,1), ylim=c(0,1), zlim=c(0,1))
##############
Your theory that you don't need z, because it can be computed from x and y is flawed.
If it were that way, then x,y and x,z and y,z would all give the same result.
But the algorithms don't assume x+y+z=1. They won't assume x*x+y*y+z*z=1 either or other dependencies of features.

In elm, how can I pipe a value to a function as an argument other than the last one?

I'm new to elm and to be honest struggling a bit to get my head around certain concepts right now. I'm not sure how clear my question is but this is what I'm trying to do.
for example:
aFunction value1 value2
is equivalent to:
value2
|> aFunction value1
but what if I want to pass value1 to aFunction through a pipe instead of value2?
at the moment I'm using something like this:
value1
|> (\x y -> aFunction y x) value2
However, it strikes me as a bit of a kludge, to be honest. Is there a more elegant way to do this?
What I'm trying to code in practice is part of quite a long chain of pipes which would be impractical (or at least unreadable) to do using an expression with lots of parentheses.
Use the flip function (which is just the function you are defining in-line with the lambda expression):
value1 |> (flip aFunction) value2
As per #chepner's answer, flip seems to be the best way to do this for functions with 2 arguments.
Given that there doesn't seem to be any built in solution to the wider question of how to do this for functions having more than 2 arguments, I came up with the following.
I'm really not sure if this is likely to be particularly useful in real life, but it was (at least) an interesting exercise for me as I'm really still learning fairly basic stuff:
module RotArgs exposing (..)
rot3ArgsL1 func = \y z x -> func x y z
rot3ArgsL2 func = \z x y -> func x y z
rot4ArgsL1 func = \x y z w -> func w x y z
rot4ArgsL2 func = \y z w x -> func w x y z
rot4ArgsL3 func = \z w x y -> func w x y z
So, for example rot3ArgsL1 takes a 3-argument function and returns a version of that function in which the expected order of arguments has been rotated 1 place to the left... and so forth.
It would enable things like:
import RotArgs exposing (..)
someFunction one two three =
...whatever
...
one
|> (rot3ArgsL1 someFunction) two three
Obviously RotArgs could easily be extemded to functions of more than 4 arguments, though that seems likely to be increasingly less useful the more arguments are added.

Prolog Program to Find Square of Natural Numbers

My code below is meant to generate the square of natural numbers in order
(i.e sq(X). -> X=0; X=1; X=4; X=9; X=16; ...)
nat(0).
nat(X) :- nat(Y), Z is Y+1, X is Z*Z.
but the answer I am getting is:
1
0 ?- nat(X).
X = 0 ;
X = 1 ;
X = 4 ;
X = 25 ;
X = 676
Should be a quick fix, but I've spent longer on this than I'd like to say. Any help is greatly appreciated!
your nat/1 really seems to return a different sequence. Should be
nat(0).
nat(X) :- nat(Y), X is Y+1.
and then, a different predicate for square
sq(X) :- % call nat/1, square it...
please complete the code

How would I split a large set of tabular data into smaller relevant tables? (Not a DB Question)

I'm really hoping I can describe this question in an understandable way. This is a puzzle that I have not been able to begin to solve even though I (mostly) understand it. I'm just not sure where to start, and I'm really hoping someone out there can get me headed in the right direction.
I have a LARGE table of data. It describes relationships between objects. Let's say the Y-axis has items numbered 1-1000, and the X-axis has items 1-1000 also. If item #234 on the Y-axis is related to item #791 on X, there will be a mark in the table where the row and column cross. In some industries this is referred to an a Truth Table. One can, at a glance, see how many items in a system relate to each other. The marks in the table can help to identify trends and patterns.
Here's some other helpful stuff about the nature of the table:
The full range of the number of relationships (r) for each item on either axis can be 1 <= r <= axisTotal.
The X and Y axis will share common items, but each axis will also have items that the other axis does not.
Each item will only exist once per axis. It can be on X and Y, but it would only be on each one 1 time.
The total number of items on each axis will most likely NOT be equal. Each axis could have from 50 to 1000's of items.
The end result is that this is going to be a report that needs to be printed. We have successfully printed a table that had about 100-150 items on each axis on an 11in X 17in piece of paper. Any more than that and it begins to be so small it's unreadable.
What I am trying to do is split the super large tables into smaller tables, but related points need to stay together. If I grab item 1-100 on X then I would need each item they relate to from Y.
I've generated a number of these tables and, while the number of relationships CAN be arbitrary, I have never seen an item relate to all other items. So in real practice the range is more like 1 <= r <= (10% * axisTotal). If an item's relationships exceed this range, it can be split up into multiple tables, but that is not optimal at all.
At the end of the day I think we, and our clients, would be happy if a 1000x1000 item table was split into 8 to 10 printed pages of smaller, related tables.
Any guidance would be a great help! Thanks.
---EDIT---
One other thing worth noting, there will be no empty rows or columns in the table. Every item on both the x and y axis will relate to at least 1 item on the opposite axis.
---EDIT---
Here is an example of a small truth table that I'm describing: . Every row and column has at least one relationship.
---EDIT---
May 18th, 2011
For what it's worth, I was moving pretty good on this project and I got pulled off for a couple of weeks. So it's going to a little while before I get back to this problem. But it is one that I will have to solve soon.
---EDIT---
July 11th, 2011
Bummer. Well, looks like I'm not going to be able to solve this problem right now. I was really hoping to be able to figure this out. Through discussion we decided to present the truth table in an Excel spreadsheet as an add-on resource to the main report. Excel 2007 and later will handle 1000's of columns which will more than suffice. Plus, we added some VBA which allows the viewer to double click on the column titles. This action will reduce the rows to only ones where there are interactions. Then it removes empty columns. In this way they can see a small sub-table based on the item they want to view, and can print it if they want.
This isn't an answer, I just want to try to visualize your data a little better. Does it kind of look like this?
Alice Bob Charlie ... Zelda
Shoes X X
Hats X X
Gloves X
...
Pants X
EDIT
Is it a requirement to show the data in tabular format? Or could you just list each out? Something like:
Alice
Shoes
Bob
Hats
Pants
Charlie
Shoes
Gloves
Zelda
Hats
Or the other way:
Shoes
Alice
Charlie
Hats
Bob
Zelda
Gloves
Charlie
Pants
Bob
EDIT 2
Okay, I've made another larger truth table to hopefully get a better understanding of how you want to split things up:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 x x x x
2 x x x x x x
3 x x x x
4 x x x
5 x x x
6 x x x
7 x x x
8 x x x
For argument's sake lets just say that you can only fit 4 rows on a page (because I don't feel like typing out a giant table this early in the morning) so we're going to split this into two pages. First, it is important to show every row, right? Second, do you need to show columns that never have a value. For instance Y and Z never have a value for rows 1 through 8 in this table, can they be excluded from the report or do they still need to be there? Third, is order of the rows important?
If its not important to show completely empty columns then we could remove 10 columns from the table above and compress it down to:
A B C E F H I L M O P Q R U V W
1 x x x x
2 x x x x x x
3 x x x x
4 x x x
5 x x x
6 x x x
7 x x x
8 x x x
Then if row order isn't important you can compress it further by taking an optimum row arrangement (not necessarily shown here). The two tables below have further been compress to 11 and 10 columns:
A B C F H I M P Q R U
1 x x x x
2 x x x x x x
5 x x x
7 x x x
A E H I L M O P U W
3 x x x x
4 x x x
6 x x x
8 x x x
Am I going down a completely wrong path here? These are all just questions to help me better understand your data and output requirements.
Also, in all seriousness, is it an option to get larger printers/plotters? Also, is it an option to just generate a PDF and use Acrobat's print tile's option?
Last year I read an article at the Computational Biology PLoS journal (www.ploscompbiol.org), that seems related to your problem.
In short, it describes a new approach when we already have a set of proteins and tabular data about their one-to-one interaction and we want to to group them so that interaction inside a group and interaction between two groups is either maximized or (this is the innovative idea) minimized .
If we plot the start data table with black for high and white for low interaction it looks randomly gray. The result table, after the calculations and rearranging is done (so grouped items are placed near one another), looks more like orthogonal areas of black and white.
The article: Protein Interaction Networks—More Than Mere Modules,
where there are also references to other older techniques for grouping this kind of data.