PLearn 0.1
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Private Types
PLearn::IfThenElseVariable Class Reference

Variable that represents the element-wise IF-THEN-ELSE: More...

#include <IfThenElseVariable.h>

Inheritance diagram for PLearn::IfThenElseVariable:
Inheritance graph
[legend]
Collaboration diagram for PLearn::IfThenElseVariable:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 IfThenElseVariable ()
 Default constructor for persistence.
 IfThenElseVariable (Var IfVar, Var ThenVar, Var ElseVar)
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual IfThenElseVariabledeepCopy (CopiesMap &copies) const
virtual void build ()
 Post-constructor.
virtual void recomputeSize (int &l, int &w) const
 Recomputes the length l and width w that this variable should have, according to its parent variables.
virtual void fprop ()
 compute output given input
virtual void bprop ()
virtual void symbolicBprop ()
 compute a piece of new Var graph that represents the symbolic derivative of this Var
virtual void rfprop ()
VarIf ()
VarThen ()
VarElse ()

Static Public Member Functions

static string _classname_ ()
 IfThenElseVariable.
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static Object_new_instance_for_typemap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

void build_ ()
 Object-specific post-constructor.

Private Types

typedef NaryVariable inherited

Detailed Description

Variable that represents the element-wise IF-THEN-ELSE:

Definition at line 58 of file IfThenElseVariable.h.


Member Typedef Documentation

Reimplemented from PLearn::NaryVariable.

Definition at line 60 of file IfThenElseVariable.h.


Constructor & Destructor Documentation

PLearn::IfThenElseVariable::IfThenElseVariable ( ) [inline]

Default constructor for persistence.

Definition at line 64 of file IfThenElseVariable.h.

{}
PLearn::IfThenElseVariable::IfThenElseVariable ( Var  IfVar,
Var  ThenVar,
Var  ElseVar 
)

Definition at line 61 of file IfThenElseVariable.cc.

References build_().

    : inherited(IfVar & ThenVar & (VarArray)ElseVar,ThenVar->length(), ThenVar->width())
{
    build_();
}

Here is the call graph for this function:


Member Function Documentation

string PLearn::IfThenElseVariable::_classname_ ( ) [static]

IfThenElseVariable.

Reimplemented from PLearn::NaryVariable.

Definition at line 59 of file IfThenElseVariable.cc.

OptionList & PLearn::IfThenElseVariable::_getOptionList_ ( ) [static]

Reimplemented from PLearn::NaryVariable.

Definition at line 59 of file IfThenElseVariable.cc.

RemoteMethodMap & PLearn::IfThenElseVariable::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::NaryVariable.

Definition at line 59 of file IfThenElseVariable.cc.

bool PLearn::IfThenElseVariable::_isa_ ( const Object o) [static]

Reimplemented from PLearn::NaryVariable.

Definition at line 59 of file IfThenElseVariable.cc.

Object * PLearn::IfThenElseVariable::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Definition at line 59 of file IfThenElseVariable.cc.

StaticInitializer IfThenElseVariable::_static_initializer_ & PLearn::IfThenElseVariable::_static_initialize_ ( ) [static]

Reimplemented from PLearn::NaryVariable.

Definition at line 59 of file IfThenElseVariable.cc.

void PLearn::IfThenElseVariable::bprop ( ) [virtual]

Implements PLearn::Variable.

Definition at line 120 of file IfThenElseVariable.cc.

References Else(), PLearn::fast_exact_is_equal(), PLearn::Variable::gradient, PLearn::Variable::gradientdata, If(), PLearn::Variable::isScalar(), PLearn::Variable::nelems(), Then(), and PLearn::Variable::valuedata.

{
    if(If()->isScalar())
    {
        if (!fast_exact_is_equal(If()->valuedata[0], 0))
            Then()->gradient += gradient;
        else
            Else()->gradient += gradient;
    }
    else
    {
        real* ifv = If()->valuedata;
        real* theng = Then()->gradientdata;
        real* elseg = Else()->gradientdata;
        for (int k=0;k<nelems();k++)
        {
            if ( !fast_exact_is_equal(ifv[k], 0)  )
                theng[k] += gradientdata[k];
            else
                elseg[k] += gradientdata[k];
        }
    }
}

Here is the call graph for this function:

void PLearn::IfThenElseVariable::build ( ) [virtual]

Post-constructor.

The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.

Reimplemented from PLearn::NaryVariable.

Definition at line 68 of file IfThenElseVariable.cc.

References PLearn::NaryVariable::build(), and build_().

Here is the call graph for this function:

void PLearn::IfThenElseVariable::build_ ( ) [protected]

Object-specific post-constructor.

This method should be redefined in subclasses and do the actual building of the object according to previously set option fields. Constructors can just set option fields, and then call build_. This method is NOT virtual, and will typically be called only from three places: a constructor, the public virtual build() method, and possibly the public virtual read method (which calls its parent's read). build_() can assume that its parent's build_() has already been called.

Reimplemented from PLearn::NaryVariable.

Definition at line 75 of file IfThenElseVariable.cc.

References PLearn::Variable::isScalar(), PLearn::TVec< T >::length(), PLearn::Variable::length(), PLERROR, PLearn::TVec< T >::size(), and PLearn::NaryVariable::varray.

Referenced by build(), and IfThenElseVariable().

{
    if (varray.size()) {
        if (varray[1]->length() != varray[2]->length() || varray[1]->width() != varray[2]->width())
            PLERROR("In IfThenElseVariable: ElseVar and ThenVar must have the same size");
        if (!varray[0]->isScalar() && (varray[0]->length() != varray[1]->length() || varray[0]->width() != varray[1]->width()))
            PLERROR("In IfThenElseVariable: IfVar must either be a scalar or have the same size as ThenVar and ElseVar");
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

string PLearn::IfThenElseVariable::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 59 of file IfThenElseVariable.cc.

static const PPath& PLearn::IfThenElseVariable::declaringFile ( ) [inline, static]

Reimplemented from PLearn::NaryVariable.

Definition at line 67 of file IfThenElseVariable.h.

{ return varray[0]; }
IfThenElseVariable * PLearn::IfThenElseVariable::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::NaryVariable.

Definition at line 59 of file IfThenElseVariable.cc.

Var& PLearn::IfThenElseVariable::Else ( ) [inline]

Definition at line 78 of file IfThenElseVariable.h.

Referenced by bprop(), fprop(), rfprop(), and symbolicBprop().

{ return varray[2]; }

Here is the caller graph for this function:

void PLearn::IfThenElseVariable::fprop ( ) [virtual]

compute output given input

Implements PLearn::Variable.

Definition at line 94 of file IfThenElseVariable.cc.

References Else(), PLearn::fast_exact_is_equal(), If(), PLearn::Variable::isScalar(), PLearn::Variable::nelems(), PLearn::test(), Then(), PLearn::Variable::value, and PLearn::Variable::valuedata.

{
    if(If()->isScalar())
    {
        bool test = !fast_exact_is_equal(If()->valuedata[0], 0);
        if (test)
            value << Then()->value;
        else
            value << Else()->value;
    }
    else
    {
        real* ifv = If()->valuedata;
        real* thenv = Then()->valuedata;
        real* elsev = Else()->valuedata;
        for (int k=0;k<nelems();k++)
        {
            if ( !fast_exact_is_equal(ifv[k], 0) )
                valuedata[k]=thenv[k];
            else
                valuedata[k]=elsev[k];
        }
    }
}

Here is the call graph for this function:

OptionList & PLearn::IfThenElseVariable::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 59 of file IfThenElseVariable.cc.

OptionMap & PLearn::IfThenElseVariable::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 59 of file IfThenElseVariable.cc.

RemoteMethodMap & PLearn::IfThenElseVariable::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Definition at line 59 of file IfThenElseVariable.cc.

Var& PLearn::IfThenElseVariable::If ( ) [inline]

Definition at line 76 of file IfThenElseVariable.h.

Referenced by bprop(), fprop(), rfprop(), and symbolicBprop().

{ return varray[0]; }

Here is the caller graph for this function:

void PLearn::IfThenElseVariable::recomputeSize ( int l,
int w 
) const [virtual]

Recomputes the length l and width w that this variable should have, according to its parent variables.

This is used for ex. by sizeprop() The default version stupidly returns the current dimensions, so make sure to overload it in subclasses if this is not appropriate.

Reimplemented from PLearn::Variable.

Definition at line 85 of file IfThenElseVariable.cc.

References PLearn::TVec< T >::length(), PLearn::TVec< T >::size(), and PLearn::NaryVariable::varray.

{
    if (varray.size()) {
        l = varray[1]->length();
        w = varray[1]->width();
    } else
        l = w = 0;
}

Here is the call graph for this function:

void PLearn::IfThenElseVariable::rfprop ( ) [virtual]

Reimplemented from PLearn::Variable.

Definition at line 153 of file IfThenElseVariable.cc.

References Else(), PLearn::fast_exact_is_equal(), If(), PLearn::TVec< T >::length(), PLearn::Variable::nelems(), PLearn::NaryVariable::resizeRValue(), PLearn::Variable::rValue, PLearn::Variable::rvaluedata, Then(), and PLearn::Variable::valuedata.

{
    if (rValue.length()==0) resizeRValue();
    if(If()->isScalar())
    {
        if (!fast_exact_is_equal(If()->valuedata[0], 0))
            rValue << Then()->rValue;
        else
            rValue << Else()->rValue;
    }
    else
    {
        real* ifv = If()->valuedata;
        real* rthenv = Then()->rvaluedata;
        real* relsev = Else()->rvaluedata;
        for (int k=0;k<nelems();k++)
        {
            if ( !fast_exact_is_equal(ifv[k], 0) )
                rvaluedata[k]=rthenv[k];
            else
                rvaluedata[k]=relsev[k];
        }
    }
}

Here is the call graph for this function:

void PLearn::IfThenElseVariable::symbolicBprop ( ) [virtual]

compute a piece of new Var graph that represents the symbolic derivative of this Var

Reimplemented from PLearn::Variable.

Definition at line 145 of file IfThenElseVariable.cc.

References Else(), PLearn::Variable::g, If(), PLearn::ifThenElse(), PLearn::Variable::length(), Then(), PLearn::Variable::width(), and zero.

{
    Var zero(length(), width());
    Then()->accg(ifThenElse(If(), g, zero));
    Else()->accg(ifThenElse(If(), zero, g));
}

Here is the call graph for this function:

Var& PLearn::IfThenElseVariable::Then ( ) [inline]

Definition at line 77 of file IfThenElseVariable.h.

Referenced by bprop(), fprop(), rfprop(), and symbolicBprop().

{ return varray[1]; }

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from PLearn::NaryVariable.

Definition at line 67 of file IfThenElseVariable.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines