Linear::getParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
namespace MachineLearning\Domain\Hypothesis;
3
4
use MachineLearning\Domain\Exception\WrongVariableForHypothesisException;
5
use MachineLearning\Domain\Model\Result;
6
use MachineLearning\Domain\Model\Value\VectorValue;
7
use MachineLearning\Domain\Model\Value\ScalarValue;
8
use MachineLearning\Domain\Model\ValueInterface;
9
10
class Linear implements HypothesisInterface
11
{
12
13
    /**
14
     * @inheritdoc
15
     */
16 2
    public function calculate(ValueInterface $coefficient, ValueInterface $variable)
17
    {
18 2
        if (! $variable instanceof VectorValue) {
19
            throw new WrongVariableForHypothesisException();
20
        }
21
22 2
        list($offset, $firstOrderCoefficients) = $this->getParameters($coefficient, $variable);
23
24 2
        $value = $offset+$variable->scalar($firstOrderCoefficients);
25 2
        return new ScalarValue($value);
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 2
    public function derivative(ValueInterface $coefficient, ValueInterface $variable, $partialVariable)
32
    {
33 2
        if (! $variable instanceof VectorValue) {
34
            throw new WrongVariableForHypothesisException();
35
        }
36
37 2
        $x = $variable->getValue();
38
39 2
        return new ScalarValue($x[$partialVariable]);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 2
    public function createResultFromData(array $data)
46
    {
47 2
        return new Result(
48 2
            new VectorValue($data[0]),
49 2
            new ScalarValue($data[1])
50 2
        );
51
    }
52
53
54
    /**
55
     * @param ValueInterface $coefficient
56
     * @param ValueInterface $variable
57
     * @return array
58
     */
59 2
    protected function getParameters(ValueInterface $coefficient, ValueInterface $variable)
0 ignored issues
show
Unused Code introduced by
The parameter $variable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61 2
        $coefficientValues = $coefficient->getValue(); //e.g. [1,1,3]
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
63 2
        $offset = $coefficientValues[0];
64 2
        array_shift($coefficientValues);
65
66 2
        $firstOrderCoefficients = new VectorValue($coefficientValues);
67
68 2
        return array($offset, $firstOrderCoefficients);
69
    }
70
}
71