0

I have two tables: Table 1 (tb1): Shows which symbols belong to which blocks (1 = belongs, NULL = doesn't belong)

SYMBOL | block1 | block2 | block3 | block4
-------|--------|--------|--------|-------
sym1   |        |        | 1      |      
sym2   |        | 1      |        | 1
sym3   | 1      |        |        |      
sym4   |        |        | 1      |      
sym5   |        |        |        | 1

Table 2 (tb2): Contains three numerical features for some symbols (note: not all symbols are included)

SYMBOL | col1 | col2 | col3
-------|------|------|------
sym1   | 1.1  | 2.1  | 3.1
sym3   | 1.2  | 2.2  | 3.2
sym4   | 1.3  | 2.3  | 3.3

For each block in tb1:

  • Find all symbols marked with 1.

  • For those symbols, look up their feature values (col1, col2, col3) in tb2 (if they exist).

  • Calculate the inner product between:

    • The weight vector (from tb1's block column)
    • Each feature column (col1/col2/col3) from tb2

For example:

  • block1: Only sym3 is marked → Use its values (1.2, 2.2, 3.2).
  • block3: sym1 + sym4 → Sum their values: (1.1+1.3, 2.1+2.3, 3.1+3.3) = (2.4, 4.4, 6.4).
  • block2/block4: Their symbols (sym2, sym5) aren’t in tb2 → Return empty.

Here is my expected output:

        col1 col2 col3             
        ---- ---- -----
block1| 1.2  2.2  3.2              
block2|                                        
block3| 2.4  4.4  6.4
block4|

How to implement this inner product calculation in DolphinDB?

1 Answer 1

0

You can achieve this calculation using matrix operations. Here's how to implement it step by step:
First, convert the data into labeled matrices.

tb1 = table(`sym1`sym2`sym3`sym4`sym5 as SYMBOL, NULL NULL 1 NULL NULL as block1, NULL 1 NULL NULL NULL as block2, 1 NULL NULL 1 NULL as block3, NULL 1 NULL NULL 1 as block4)
tb2 = table(`sym1`sym3`sym4 as SYMBOL, 1+double(seq(1,3))/10 as col1, 2+double(seq(1,3))/10 as col2, 3+double(seq(1,3))/10 as col3)

m1 = matrix(tb1[:,1:]).rename!(tb1.SYMBOL, tb1.colNames()[1:])
m2 = matrix(tb2[:,1:]).rename!(tb2.SYMBOL, tb2.colNames()[1:])

Then use the cross function to compute the inner products between all pairs of columns from the two matrices.

mm1, mm2 = align(m1, m2, 'fj', true)
re = cross(wsum, mm1, mm2)

If the row labels of the two matrices don't completely match, you can first align them using the align function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.