0

I am using Swix to create matrices in Swift. I form my matrix using a list of x values. This was not a problem when my list of x values was a list of integers, but when I try to use decimal numbers in my list of x values I get the error "Cannot subscript a value of type '[Double]'". Here is my code:

@IBAction func button(sender: UIButton) {
    let x_values = [3.629, 2, 3]
    let y_values:[Double] = [4, 8, 16]

    // Counters for loops
    var columns = 0
    var x_parse = 0

    // Create Swix matrix
    var mat = eye(x_values.count)

    // Assign values to matrix from x_values
    while columns < x_values.count {
        var rows = x_values.count - 1
        var exponent = x_values.count
        while rows > -1 {
            let element = x_values[x_parse] ^^ exponent

            mat[columns, rows] = element
            rows = rows - 1
            exponent = exponent - 1
        }
        columns = columns + 1
        x_parse = x_parse + 1
    }
    let y_ndarray = asarray(y_values)
    let solution = solve(mat, b: y_ndarray)
    print(String(solution))
    print(solution)
    print(label.text = String(solution))
}

The issue arose in the line:

let element = x_values[x_parse] ^^ exponent

highlighting x_values as the issue.

The code defining the ^^ operator is:

infix operator ^^ { }
func ^^ (radix: Int, power: Int) -> Int {
    return Int(pow(Double(radix), Double(power)))
}

How should I access the values in my list if it includes doubles?

Wes
  • 3
  • 4
  • 1
    Could you show us how `^^` operator is defined in Swix (or is this some operator you've defined yourself?)? This seems as a classic case of an obfuscated error: where the real source of the error is most likely not the attempt to index `x_values` but rather the use of `^^` on lhs `x_values` (type `[Double]`) and rhs `exponent` (type `Int`). – dfrib Mar 30 '16 at 07:21
  • There is no `^^` operator in the Swift standard library, this must be a custom defined one. – Martin R Mar 30 '16 at 07:26
  • Minor "edit" to my comment above: _"... use of `^^` on lhs `x_values[x_parse]` (type `Double`) and rhs `exponent` (type `Int`)"_. @MartinR: He briefly mentions [Swix](https://github.com/stsievert/swix), so possibly one defined in there (although I can't find it). – dfrib Mar 30 '16 at 07:29
  • @dfri I forgot to mention that I defined a ^^ operator earlier in my code. It is an exponent operator. – Wes Mar 30 '16 at 17:46
  • @dfri The exponent operator has been working just fine up until now. I also tried casting the rhs 'exponent' as a double with the double function provided by Swift 'Double(exponent)' and got the same error. – Wes Mar 30 '16 at 17:57
  • @Wes Could you possibly include the definition of this operator in your question? I still belive this custom operator is the source of your error. – dfrib Mar 30 '16 at 18:41
  • @dfri I've edited the question to show the definition of my ^^ operator. I think you are right about that being the problem, and I am just testing a potential solution now. – Wes Mar 30 '16 at 19:16
  • @Wes: is this the only version of this operator you have? And with this, you still get the error _"Cannot subscript a value of type '[Double]' in Swift"_? If you only have one `^^` as above, you won't be able to call it with a `lhs` as `Double`, and the compiler should prompt you with an error _"cannot convert value of type 'Double' to expected argument type 'Int'"_, rather than the subscript error. – dfrib Mar 30 '16 at 19:23
  • @dfri I replaced the ^^ operator with another that I found. It looks like I got everything working how I want. I will post my solution as an answer to the question. Thanks! – Wes Mar 30 '16 at 19:32

1 Answers1

0

It seems that the issue arose from the definition of the ^^ operator. I replaced the code defining the ^^ operator with another snippet I found. The new operator definition is:

infix operator ** { associativity left precedence 170 }

func ** (num: Double, power: Double) -> Double{
    return pow(num, power)
}

and it is working when I make my x_values a list of doubles:

let x_values:[Double] = [3.629, 3.561, 3.527]

and update my code like so:

let element = x_values[x_parse] ** exponent

Everything seems to be working properly now.

Wes
  • 3
  • 4