OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TimesLink.cc
Go to the documentation of this file.
1 /*
2  * opencog/atoms/reduct/TimesLink.cc
3  *
4  * Copyright (C) 2015 Linas Vepstas
5  * All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License v3 as
9  * published by the Times Software Foundation and including the exceptions
10  * at http://opencog.org/wiki/Licenses
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program; if not, write to:
19  * Times Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <opencog/atomspace/atom_types.h>
26 #include "TimesLink.h"
27 
28 using namespace opencog;
29 
33  : ArithmeticLink(TIMES_LINK, oset, tv, av)
34 {
35  init();
36 }
37 
41  : ArithmeticLink(t, oset, tv, av)
42 {
43  if (not classserver().isA(t, TIMES_LINK))
44  throw InvalidParamException(TRACE_INFO, "Expecting a TimesLink");
45  init();
46 }
47 
51  : ArithmeticLink(t, a, b, tv, av)
52 {
53  if (not classserver().isA(t, TIMES_LINK))
54  throw InvalidParamException(TRACE_INFO, "Expecting a TimesLink");
55  init();
56 }
57 
61  : ArithmeticLink(TIMES_LINK, a, b, tv, av)
62 {
63  init();
64 }
65 
67  : ArithmeticLink(l)
68 {
69  Type tscope = l.getType();
70  if (not classserver().isA(tscope, TIMES_LINK))
71  throw InvalidParamException(TRACE_INFO, "Expecting a TimesLink");
72  init();
73 }
74 
75 void TimesLink::init(void)
76 {
77  knild = 1.0;
79 }
80 
81 // ============================================================
82 
83 double TimesLink::konsd(double a, double b) const { return a*b; }
84 
85 static inline double get_double(const Handle& h)
86 {
88  if (NULL == nnn)
89  nnn = createNumberNode(*NodeCast(h));
90 
91  return nnn->get_value();
92 }
93 
97 Handle TimesLink::kons(const Handle& fi, const Handle& fj)
98 {
99  // Are they numbers?
100  if (NUMBER_NODE == fi->getType() and
101  NUMBER_NODE == fj->getType())
102  {
103  double prod = get_double(fi) * get_double(fj);
104  return Handle(createNumberNode(prod));
105  }
106 
107  // If we are here, we've been asked to multiply two things of the
108  // same type, but they are not of a type that we know how to multiply.
109  return Handle(createTimesLink(fi, fj)->reorder());
110 }
111 
112 // ============================================================
friend class Handle
Definition: Atom.h:79
std::vector< Handle > HandleSeq
a list of handles
Definition: Handle.h:246
std::shared_ptr< TruthValue > TruthValuePtr
Definition: TruthValue.h:85
std::shared_ptr< AttentionValue > AttentionValuePtr
Type getType() const
Definition: Atom.h:197
#define createTimesLink
Definition: TimesLink.h:71
ClassServer & classserver(ClassServerFactory *=ClassServer::createInstance)
Definition: ClassServer.cc:159
static NodePtr NodeCast(const Handle &h)
Definition: Node.h:113
std::shared_ptr< NumberNode > NumberNodePtr
Definition: NumberNode.h:76
static NumberNodePtr NumberNodeCast(const Handle &h)
Definition: NumberNode.h:77
unsigned short Type
type of Atoms, represented as short integer (16 bits)
Definition: types.h:40
#define createNumberNode
Definition: NumberNode.h:83