开发者

Finding variables that share common properties

开发者 https://www.devze.com 2023-03-21 19:39 出处:网络
I\'m using Mathematica and have a set of variables (A,B,C,D,...) with properties A=(blue, big, rounded), B=(red, small, spiky), and so forth. Those properties can be common between variables. Wh开发者

I'm using Mathematica and have a set of variables (A,B,C,D,...) with properties A=(blue, big, rounded), B=(red, small, spiky), and so forth. Those properties can be common between variables. Wh开发者_Python百科at would be the best, general way to find all variables that share a common property (of being, for instance, small)? Thanks.


Here's a list of possible properties:

In[1]:= properties={"red","green","blue","big","small","rounded","spiky"};

And here's a list of objects with some of those properties

In[2]:= list={{"blue","big","rounded"},{"red","small","spiky"},
              {"red","big","rounded"},{"blue","small","spiky"}};

You can find all objects that have the property of, e.g., being "blue" using Select

In[3]:= Select[list, MemberQ[#,"blue"]&]
Out[3]= {{blue,big,rounded},{blue,small,spiky}}

This could be wrapped up into a function. Although how I would write that function would depend on the data structures and usage that you're planning.


Actually, I just reread you question you have a list of objects with some properties and you want to refer to those objects by name. So you probably want something more like

In[1]:= listProperties["A"]:={"blue","big","rounded"}
        listProperties["B"]:={"red","small","spiky"}
        listProperties["C"]:={"red","big","rounded"}
        listProperties["D"]:={"blue","small","spiky"}

Above I defined some properties that are associated with certain strings. You don't have to use strings in the above or below, and you can create a better structure than that if you want. You could also make a constructor to create the above, such a constructor could also check if the list of properties supplied is of the right form - i.e. does not have contradictory properties, are all in a list of known properties etc...

We then define a function to test if an object/string has a certain property associated with it

In[2]:= hasProperty[obj_, property_]:=MemberQ[listProperties[obj],property]

You might want to return an error or warning message if listProperties[obj] does not have a definition/rule associated with it.

Use Select to find all "objects" in a list that have the associated property "blue":

In[3]:= Select[{"A","B","C","D"}, hasProperty[#,"blue"]&]
Out[3]= {A,D}

There are other ways (probably better ways) to set up such a data structure. But this is one of the simplest ways in Mathematica.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号